Installation of PostgreSQL Linux using Source Code:
To begin, connect to the Linux virtual Machine via PuTTY using IP address. To obtain the IP address, log into the virtual machine and enter the command ‘ifconfig’ in the terminal. Now open Putty and enter the IP address of the virtual machine in the “Host Name” field and click on “open”.
You will be prompted to enter your username and password for the Linux virtual machine. Now you are ready to use Linux virtual machine through the Putty terminal.
Installation of PostgreSQL using source code method:
Prerequisites before installing the PostgreSQL:
Before you install the application, it is recommended to check few prerequisites.
-
[root@postgres1 ~]# make -version
GNU Make 3.81
This program built for x86_64-redhat-linux-gnu
-
[root@postgres1 ~]# rpm -qagcc
gcc-4.4.7-23.0.1.el6.x86_64
There are also some Optional prerequisites that you may want to check:
-
[root@postgres1 ~]# rpm -qareadline
readline-6.0-4.el6.x86_64
-
[root@postgres1 ~]# rpm -qazlib
zlib-1.2.3-29.el6.x86_64
Source code installation:
- Download the software
- Firstly,In terminal window of Linux system, Navigate to the directory where you want to download the PostgreSQL source code.
- Use ‘wget’ command to download the source code tar file for PostgreSQL v11.0.you can get the link from the official website:https://www.postgresql.org/ftp/source/ and select the version you want to install Here we install v11.0. Download the source code tar file for your platform and version.
[root@postgres1 ~]#wgethttps://ftp.postgresql.org/pub/source/v11.0/postgresql-11.0.tar.gz
- Extract the source code:
If you download the tar file, a directory named ‘postgresql-11.0.tar.gz’, you can extract it by running the command:
[root@postgres1 ~]# tar –xvf postgresql-11.0.tar.gz (untar the file).
After extracting thetar file, a directory named ‘postgresql-11.0’ will be created.
- Configure PostgreSQL:
Navigate into the extracted directory:
[root@postgres1 ~]# cd postgresql-11.0
[root@postgres1 postgresql-11.0]#
Now, the ‘configure’ command will check your system for dependencies and set up the build environment.
[root@postgres1 postgresql-11.0]# ./configure
- Build and install PostgreSQL:
Run the ‘make’ command will compile the source code into executable files.
[root@postgres1 postgresql-11.0]#make
Run the ‘make install’ command install PostgreSQL on your system.
[root@postgres1 postgresql-11.0]#make install
PostgreSQL installation complete.
- Initialize the database cluster:
- Change directory to ‘pgsql’ by using command:
1 |
[root@postgres1 postgresql-11.0]# cd /usr/local/pgsql |
- Now check the files in pgsql by using command
1 |
[root@postgres1 pgsql]# ls |
bin include lib share
- To check the directories run the following commands:
1 2 3 4 |
[root@postgres1 pgsql]# cd bin/ [root@postgres1 bin]#ll [root@postgres1 pgsql]# cd lib/ [root@postgres1 lib]#ll |
- Change the directory to where you want to create PostgreSQL data directory
1 |
[root@postgres1 pgsql]# cd /u01 |
- Now create a data directory for PostgreSQL, Here we create the directory ‘/u01/data’ by using following command:
1 |
[root@postgres1 u01]#mkdir -p /u01/data |
- Set the owner and group of the directory to the ‘postgres’ user:
1 |
[root@postgres1 u01]#chown -R postgres:postgres /u01/data1 |
- Switch to the ‘postgres’ user by running this command:
1 2 |
[root@postgres1 u01]#su–postgres [postgres@postgres1 ~]$ |
This will change your user to ‘postgres’ and put you in the home directory of the user.
- Run the ‘initdb’ command to initialize the PostgreSQL data directory and also used to create a cluster:
1 |
[postgres@postgres1 ~]$ /usr/local/pgsql/bin/initdb -D /u01/data1 |
The files belonging to this database system will be owned by user “postgres”.
This user must also own the server process.
- Start the cluster
Start the PostgreSQL server using the ‘pg_ctl’ command:
1 |
[postgres@postgres1 ~]$ /usr/local/pgsql/bin/pg_ctl -D /u01/data1 start |
This will start the PostgreSQL server with the data directory that was initialized in the previous step.
1 |
[postgres@postgres1 ~]$ps -efa|greppostgres |
This command will verify cluster information i.e list of processes running on your system. If the PostgreSQL server is running, you should see a process with the name “postgres”.
Now connect to psql command-line interface, to run the sql commands and interact with the PostgreSQL server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
[postgres@postgres1 ~]$ /usr/local/pgsql/bin/psql Psql (11.0) Now check the PostgreSQL by running few commands below: postgres=# select version(); version ------------------------------------------------------------------------------------------ PostgreSQL 11.0 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-23.0.1), 64-bit (1 row) postgres=# select current_date; current_date -------------- 2023-03-31 (1 row) postgres=# select 10+2; ?column? ---------- 12 (1 row) Run the SQL commands to create and view the Databases in PostgreSQL postgres- CREATE DATABASE dbname; CREATE DATABASE postgres=# \1 List of databases Name | Owner | Encoding | Collate | ctype | Access privileges | -------------------------------------------------------------------------------------- dbname | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres postgres-CTc/postgres templatel | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres postgres-CTc/postgres(4 ROWS) In the above ‘dbname’ is user defined name for the database that can be replaced. The ‘\l’command is used to list all the databases,and There are three default databases in PostgreSQL. To quit the psql command-line interface use the command: postgres=# \q your session will be terminated and return to the regular command prompt. [postgres@postgres1 ~]$ Run the ‘pg_ctl’ command to stop the PostgreSQL server running on the host “postgres1” and the data directory “/u01/data”. [postgres@postgres1 ~]$ /usr/local/pgsql/bin/pg_ctl -D /u01/data1 stop server stopped |
Author : Prudhvi Teja |
LinkedIn : http://linkedin.com/in/prudhvi-teja-nagabhyru-715052224
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTExperts is always active on social media platforms.
Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform