Signup/Sign In

How to Install PostgreSQL on Debian 9

postgreSQL

PostgreSQL is an open-source object-relational database system. It is one of the top database servers used for production servers. This article will allow you to install the PostgreSQL database server on Debian 9 Stretch PCs.

Step 1 – Enable Apt Repository

First, you need to import PostgreSQL packages signing key on your machine. Use the below command to import the key.

wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -

Now add PostgreSQL apt repository on your system as per your operating system. These are indicated on the official PostgreSQL website using the following command.

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main" >> /etc/apt/sources.list.d/pgdg.list

Step 2 – Install PostgreSQL on Debian 9

At this moment, you have successfully added the PostgreSQL official repository to your system. Now update the repository list. After that, install Latest PostgreSQL Server in our Ubuntu system using the following instructions.

sudo apt-get update
sudo apt-get install PostgreSQL PostgreSQL-contrib

Step 3 – Connect to PostgreSQL

After installing the PostgreSQL database server creates a user ‘Postgres’ with the role ‘Postgres’. It also creates a system account with the same name ‘Postgres’. So to connect to the Postgres server, log in to your PC as a user of Postgres and connect the database.

sudo su - postgres \spsql

Now you are logged in to the PostgreSQL database server. Use the following command from the database command prompt to verify log-in details.

postgres-# \conninfo

To disconnect from the PostgreSQL database command prompt, write the below command and press enter. It will bring you back to the Debian command prompt.

postgres-# \q

Step 4 – Conclusion

Your PostgreSQL installation has been finished successfully on your Debian 9 machine.