Home » Blog » Setup Postgres and pgAdmin on Ubuntu 18.04

Setup Postgres and pgAdmin on Ubuntu 18.04

In this tutorial let’s look on how to setup postgres and pgAdmin on Ubuntu 18.04.

Step 1: Add PostgreSQL Repository


To install from the official repository, you first need to add it to your system.
Import the GPG repository key with the commands:

$ sudo apt-get install wget ca-certificates
$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

Then, add the PostgreSQL repository by typing:

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

Step 2: Update the Package List
After adding the official PostgreSQL repository, make sure to update the package list. Doing this ensures you install the latest PostgreSQL package.

$ sudo apt-get update

Step 3: Install PostgreSQL

To install PostgreSQL and the PostgreSQL contrib package (which provides additional features), use the following command:

$ sudo apt-get install postgresql postgresql-contrib

Step 4: Connect to PostgreSQL

To establish a connection with the newly set-up database, log into the postgres account with:

$ sudo su - postgres

Now open a postgress prompt using the command:

$ psql

Check Connection Information


If you are connected to PostgreSQL and want to see details of the connection, use the command:

\conninfo

The output displays information on the database name, the account you are logged in, the socket path, and port number.

2. Setup PgAdmin

Setup repository

Install the public key for the repository (if not done previously):

$ sudo curl https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo apt-key add

Create the repository configuration file:

$ sudo sh -c 'echo "deb https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list && apt update'

Install pgAdmin

Once, you have postgres Installed, Its time to have a GUI for managing our database, pgAdmin is a popular open source GUI recommended for postgres. Let’s install it. You can choose from desktop and web modes installation.

Install for both desktop and web modes (Recommended):

$ sudo apt install pgadmin4

Install for desktop mode only:

$ sudo apt install pgadmin4-desktop

Install for web mode only:

$ sudo apt install pgadmin4-web

Configure the webserver, if you installed pgadmin4-web:

$ sudo /usr/pgadmin4/bin/setup-web.sh