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

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay