How To Install PostgreSQL Server On Ubuntu 20.04 / 19.10

How to install and configure the PostgreSQL server in Ubuntu. This is an open-source relational database management system and it’s designed to handle workloads, from a single machine to data warehouses. In a similar way, we can configure and install PostgreSQL on Linux as well as ubuntu.

We don’t need to add any repository to install the database. we have to update the packages and simply run the below commands to get installed. 

Check Out: How To Set Java Heap Size On Tomcat Server Linux

1. If you want to update the machine before installing the PostgreSQL, you can use sudo apt update command.

2. Install the PostgreSQL using apt install postgresql postgresql-contrib postgresql-client-common

3. The service will start automatically we can check using netstat -tnlp

Check Out: How To Install Java Using Apt-Get Command In Ubuntu

Install PostgreSQL using the package:

 The PostgreSQL must be listening on port 5432. s

We can check the version installed using apt show postgresql

we can also use the below command to Check the PostgreSQL server version.

sudo -u postgres psql -c "SELECT version();"

postgresql client install ubuntu

Check Out: Enable and Disable Journaling On EXT4 File Systems In Linux

We can also check the service running with service postgresql status so

postgresql ubuntu install

By default, user postgres will be created on the machine and we can log in using the below commands. so

su - postgres

install postgresql linux

Check Out: How To Secure Apache Web With Server Hardening In Linux

you can exit using \q and check the database using \l

configure postgresql ubuntu

Check user using \du so

postgresql install in ubuntu

Installing Postgresql using source code:

We can download the PostgreSQL using the below command and extract it using the tar command. 

tar -xvf postgresql-10.13.tar.gz

Once the folder is extracted, we need to change the directory and run the following commands. 

cd postgresql-10.13/
sudo ./configure
sudo make
sudo make install

When the above command executed successfully, you will see a message” PostgreSQL installation complete“. we need to create a user and set the password.

sudo useradd postgres
sudo passwd postgres

Verify the PostgreSQL directory structure using the below command. 

ls -l /usr/local/pgsql

Now, we have to create a PostgreSQL data directory and make it owner as Postgres user. 

sudo mkdir /usr/local/pgsql/data
sudo chown postgres:postgres /usr/local/pgsql/data
sudo ls -ld /usr/local/pgsql/data/
drwxr-xr-x 2 postgres postgres 4096 Aug 29 08:09 /usr/local/pgsql/data/

Initializing the PostgreSQL database and we need to log in to the server and run the below command.

su - postgres
$ /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/                                      ## initialize the database
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /usr/local/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... America/Los_Angeles
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l logfile start

Now check the data in the data directory using sudo ls -l /usr/local/pgsql/data command. 

Use the postgres postmaster command to start the server in the background using the below command.

$ /usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data >logfile 2>&1 &

That’s it.

Share on:

I'm the founder of Curious Viral. I hope this blog will provide you complete information about Linux Technology & I would like to share my technical knowledge with you which I have learned during this period.

Other Posts You May Like...

Leave a comment