DEV Community

Dmitry Romanoff
Dmitry Romanoff

Posted on

How to Install PostgreSQL database on Ubuntu: A step-by-step guide

In this blog I will demonstrate how to install PostgreSQL database on Ubuntu.

Step #1: List the Postgres related packages that already installed

dpkg -l | grep postgres
Enter fullscreen mode Exit fullscreen mode

For example:

 dpkg -l | grep postgres
ii  postgresql-12                              12.12-0ubuntu0.20.04.1              amd64        object-relational SQL database, version 12 server
ii  postgresql-client-12                       12.12-0ubuntu0.20.04.1              amd64        front-end programs for PostgreSQL 12
ii  postgresql-client-common                   214ubuntu0.1                        all          manager for multiple PostgreSQL client versions
ii  postgresql-common                          214ubuntu0.1                        all          PostgreSQL database-cluster manager
Enter fullscreen mode Exit fullscreen mode

Step #2: In case some packages are not needed run the "sudo apt-get --purge remove ..." command to purge them:

For example:

sudo apt-get --purge remove postgresql-12 postgresql-client-12
Enter fullscreen mode Exit fullscreen mode

Step #3: Create the file repository configuration:

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
Enter fullscreen mode Exit fullscreen mode

Step #4: Import the repository signing key:

wget -qO- https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo tee /etc/apt/trusted.gpg.d/pgdg.asc &>/dev/null
Enter fullscreen mode Exit fullscreen mode

Step #5: Update the package lists:

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Step #6: Install the latest version of PostgreSQL:

sudo apt install postgresql postgresql-client -y
Enter fullscreen mode Exit fullscreen mode

Step #7: If you want a specific version, use 'postgresql-13' or similar instead of 'postgresql':

sudo apt install postgresql-13 postgresql-client-13 -y
Enter fullscreen mode Exit fullscreen mode

Step #8: Check status

sudo systemctl status postgresql
Enter fullscreen mode Exit fullscreen mode

Step #9: Check PostgreSQL client version

psql --version
Enter fullscreen mode Exit fullscreen mode

For example:

dmi@dmi-VirtualBox:~$ psql --version
psql (PostgreSQL) 13.9 (Ubuntu 13.9-1.pgdg20.04+1)
dmi@dmi-VirtualBox:~$
Enter fullscreen mode Exit fullscreen mode

Step #10: Check PostgreSQL Server version

sudo -u postgres psql
postgres=# select version();
Enter fullscreen mode Exit fullscreen mode

For example:

postgres=# select version();

                                                              version                                                          
-----------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 13.9 (Ubuntu 13.9-1.pgdg20.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, 64-bit
(1 row)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)