January 7, 2021 in Tutorial6 minutes

In this tutorial, we learn how to install PostgreSQL 10 on Ubuntu 20.04 (Focal Fossa).
PostgreSQL, or usually called Postgres, is an open-source object-relational database management system (ORDBMS) with an emphasis on extensibility and standards compliance.
PostgreSQL is ACID-compliant (Atomicity, Consistency, Isolation, Durability) and transactional. It is developed by PostgreSQL Global Development Group (PGDG) that consists of many companies and individual contributors. PostgreSQL released under the terms of PostgreSQL license.
PostgreSQL 10 was released on 5 October 2017. Major enhancements in PostgreSQL 10 includes:
This tutorial assumes you already satisfy the following requirements:
sudo privileges (recommended) or a root user access. If you’re using root user remove sudo from each command on this tutorial.Update our system to the latest update before installing PostgreSQL 10
sudo apt-get update
sudo apt-get -y upgradeAfter we upgrade our base system, now it’s time to install PostgreSQL 10.
Ubuntu 20.04 doesn’t have PostgreSQL 10 in its repository, we need to add official PostgreSQL repository so we can install PostgreSQL 10.
The PostgreSQL team is using GPG to sign downloadable packages from the PostgreSQL website.
We add PostgreSQL public GPG key so apt can verify that packages downloaded from PostgreSQL repository are not tampered or corrupt.
Add PostgreSQL GPG public key using the command below.
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -You should get OK output for command above.
If you got the different output, you need to fix the error first before continue to the next step.
After adding PostgreSQL release keys, we create a new repository configuration for PostgreSQL using the command below.
echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/postgresql-pgdg.list > /dev/nullRefresh apt metadata using the command below.
sudo apt-get updateWe’re ready to install PostgreSQL 10. Use the command below to install PostgreSQL 10 on Ubuntu 20.04.
sudo apt-get install postgresql-10Only 4 steps needed to install PostgreSQL 10 on Ubuntu 20.04. We learn how to check and verify and managing PostgreSQL 10 installation in the next few sections.
We can check PostgreSQL 10 packages that already installed using the command below.
dpkg -l | grep postgresqlBy default, PostgreSQL listens on port 5432. We can use netstat to check whether there is a process listening on port 5432.
sudo netstat -naptu | grep 5432Another method is to check whether there is a process named postgres listening on a port.
sudo netstat -naptu | grep postgresAlternative to netstat, We can use ss command below to check whether there is a process listen on PostgreSQL default port 5432.
sudo ss -atnp | grep 5432We can also use ss to check whether there is a process named postgres listening on a port.
sudo ss -atp | grep postgresThe last output above shows there is also a connection to ephemeral UDP port from local. According to this thread on PostgreSQL mailing lists, this is PostgreSQL stats collector that use UDP to send and receive stats data locally.
We can ue ps command to check what processes currently running with name contain postgres.
ps -aux | grep postgresCheck PostgreSQL 10 resource usage like CPU and memory usingtop command.
top -u postgresCheck postgres service status using command below.
sudo service postgresql statusWe can also use systemctl command to check PostgreSQL service status.
sudo systemctl status postgresqlUsing both commands above you see active (exited) on the Active: line. This happens because systemd doesn’t know the status of each PostgreSQL cluster running on the instance. One instance / installation of PostgreSQL can run mutliple clusters od PostgreSQL.
We can check, list of PostgreSQL clusters running on our machine using the command below.
pg_lsclustersThere is one cluster running on our machine. The cluster named main running PostgreSQL 10. To check this cluster service status, we can use the command below.
sudo service postgresql@10-main statusAs an alternative to service, we can use the systemctl to check this cluster status.
sudo systemctl status postgresql@10-mainStop PostgreSQL service using the command below.
sudo systemctl stop postgresqlStart PostgreSQL service using the command below.
sudo systemctl start postgresqlRestart PostgreSQL service using the command below.
sudo systemctl restart postgresqlReload PostgreSQL service using the command below.
sudo systemctl reload postgresqlNow let’s connect to the PostgreSQL 10 server using psql client application.
By default, postgres create system user named postgres. There is also role in postgresql database named postgres wth superuser permission.
Switch to postgresql user.
sudo su - postgresAfter switching to postgres user, we can connect using psql command.
To list all databases in PostgreSQL we can use \l command.
To list all users in PostgreSQL we can use \du command.
To exit from psql we can use \q command.
To exit from postgres user type exit or CTRL+D
Now we’re on our own linux user. Let’s try to login to localhost or 127.0.0.1.
$ psql -U postgres -W -h 127.0.0.1
Password for user postgres:It will prompts for a password. Leave it empty because we haven’t set any password. We get the error message below.
psql: fe_sendauth: no password suppliedSince we cannot login without providing password, we set new password for postgres user. Change user to postgres user like shown above and use psql to connect to PostreSQL.
After logging in, type \password. Enter new password and confirmation password for user postgres.
When we login again to localhost or 127.0.0.1 and provide a password, we can log in successfully.
In this section, we learn how to uninstall PostgreSQL 10 and its dependencies on Ubuntu 20.04 completely.
Please be careful when running steps in this section since this will stop your PostgreSQL database server.
Use the command below to uninstall all PostgreSQL 10 packages, both server, and client.
sudo apt-get remove postgresql-10 \
postgresql-client-10 \
postgresql-client-common \
postgresql-commonRemove the PostgreSQL data directory using the command below.
rm -rf /var/lib/postgresql/Remove PostgreSQL 10 configuration directories using the command below.
rm -rf /etc/postgresql
rm -rf /etc/postgresql-commonYou can follow the links below to learn more about PostgreSQL 10 from PostgreSQL official documentation.
In this tutorial, we learn how to install PostgreSQL 10 on ubuntu 20.04. We also learn managing PostgreSQL services, using tools like netstat, ss, ps, and top to check PostgreSQL service.
After that, we learn how to connect to PostgreSQL 10 server using psql client, changing postgres superuser password, and in the end, we learn how to uninstall PostgreSQL 10. I hope this is useful for initiating your journey to PostgreSQL 10.