Ricardo Huamani

How to install PostgreSQL and PGAdmin 4 on Ubuntu 18.04

May 13, 2020

First, we import the key from https://www.postgresql.org/media/keys/ACCC4CF8.asc

$ sudo apt install curl ca-certificates gnupg
$ curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

Then, we add the postgresql repository to our sources. For that, we create the file /etc/apt/sources.list.d/pgdg.list with the following content.

deb http://apt.postgresql.org/pub/repos/apt bionic-pgdg main

If we’re using another distribution, for example Ubuntu 16.04, we should change bionic with xenial. If we don’t know which distribution we’re using, we can run lsb_release -c in the terminal to know it.

Finally, we update the package lists and install packages. If we only wanted to install PostgreSQL and not PGAdmin, we would only specify postgresql-11.

$ sudo apt update
$ sudo apt install postgresql-11 pgadmin4

Here, we already have PostgreSQL installed and can see pgAdmin4 in our Show Applications Menu, but we need one last step.

When we installed PostgreSQL, it created a user called postgres with an empty password and a database with the same name.

To use PGAdmin we need to set a password. First, we enter into PostgreSQL CLI with the user postgres.

$ sudo -u postgres psql

psql is the command to enter into PostgreSQL CLI and make queries from the terminal. We should see the following.

psql (11.7 (Ubuntu 11.7-2.pgdg18.04+1))
Type "help" for help.

postgres=#

By default, psql tries to connect to a database with the same name of our user. This means we are connected to the database named postgres. Now we change the password of our user postgres with the next command.

ALTER USER postgres WITH PASSWORD 'your-password';

If we see ALTER ROLE, it means we changed the password successfully. To exit we just type in \q or press Ctrl+D. With this, we’ve installed PGAdmin 4 and we’re able to create connections on PGAdmin with our user postgres.

And that’s all. Thanks for reading.


Written by Ricardo Huamani Parian. Full-stack web developer, autodidact, and technical writer. I enjoy coding and sharing about technology. You can follow me on Twitter