How To Install PostgreSQL on Ubuntu & Debian

Managing databases in Linux environments is one of the best solutions to get the most out of our Ubuntu or Debian server.

What is PostgreSQL?

PostgreSQL is a robust relational database system of open source objects with more than 15 years of development allowing us to have a tool with high reliability, data integrity, and error correction.

PostgreSQL is multisystem since it can be installed in operating systems such as Linux, UNIX (AIX, BSD, HP-UX, SGI IRIX, macOS, Solaris, Tru64) and Windows.

Now we will see how to install this database manager in Debian 9.

Note: We must prefix sudo for the correct execution of the described commands.

Add PostgreSQL  APT Repository

This official PostgreSQL APT repository that we will install will be combined with the operating system offering automatic updates for all compatible versions of PostgreSQL in the Debian and Ubuntu distributions.

For this process we will create a new file by executing the following line:

/etc/apt/sources.list.d/pgdg.list

Once the file is created we will add the next line taking into account the version of Debian or Ubuntu to use:

Debian 9 Strecht: deb http://apt.postgresql.org/pub/repos/apt/ stretch-pgdg main

Debian 8 Jessie: deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main

Debian 7 Wheezy: deb http://apt.postgresql.org/pub/repos/apt/ wheezy-pgdg main

Ubuntu 17.04: deb http://apt.postgresql.org/pub/repos/apt/ zesty-pgdg main

Ubuntu 16.04: deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main

Ubuntu 14.04: deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main

Save the changes using the keys Ctrl + O, and we left the editor using Ctrl + X.

Import Keys in Debian & Ubuntu

Next, we will import the signature key of the repository and update the system package lists by first executing the following command:

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

Then, upgrade the system

sudo apt update

Install PostgreSQL Server

Once the operating system has been updated, we will run the following command for the installation of the PostgreSQL server:

apt install postgresql-9.6

In the case of Debian and Ubuntu, the database starts automatically.

The directory where we will find all PostgreSQL configuration files is located in the path:

/var/lib/postgresql/9.6/main

Start & Enable PostgreSQL Server

With the database server initialized, the next step is to start the PostgreSQL service and enable the PostgreSQL service to be automatically launched at system startup, for which we will execute the following commands in their order:

In Systemd

systemctl start postgresql.service

systemctl enable postgresql.service

sudo systemctl status postgresql.service

In SysVinit

service postgresql-9.6 start

chkconfig postgresql on

service postgresql-9.6 status

Test PostgreSQL in Debian or Ubuntu

After installing the PostgreSQL database system in Ubuntu or Debian, we can verify the installation by connecting to the Postgres database server.

The administrator user of PostgreSQL is called postgres, we will execute the following command to access the user system account:

su postgres

cd

psql

If we want to set a password for the postgregate database administrator user, we will use the following command. There we must enter and confirm the password.

\password postgres

If we want to secure the postgrey user system account, we will use the following command. There we will enter and confirm the respective password.

postgres passwd

As we see it is easy to install PostgreSQL in Debian or Ubuntu.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *