How to Install PostgreSQL in Ubuntu 20.04
PostgreSQL is a popular and powerful open source relational database management system which has been adopted for running mission critical applications.
Ubuntu includes PostgreSQL by default. To install PostgreSQL on Ubuntu, lets follow the below steps
Step 1: Update System
sudo apt update
sudo apt -y upgrade
Step 2: Install PostgreSQL Database Server
We’ll install the default version of PostgreSQL database server available on Ubuntu 20.04.4 without configuring the project’s upstream repositories.
sudo apt install postgresql postgresql-client
Confirm packages installation to proceed.
The service is automatically started upon installation. You can confirm if it is running with the command:
systemctl status postgresql.service
Step 3: Check installed version
psql -V psql
Output
psql (PostgreSQL) 12.9 (Ubuntu 12.9-0ubuntu0.20.04.1)
Step 4 – Secure PostgreSQL
PostgreSQL installer creates a user “postgres” on your system. Default this user is not protected.
Step 4.1 First, create a password for “postgres” system user account
by running the following command.
sudo passwd postgres
The prompt will ask you to enter the new password then again retype the new password. After that, a confirmation notification will be displayed ‘password updated successfully’.
After successfully changing the new password, now on each PostgreSQL access, you need to enter a new reset password for confirmation.
Step 4.2 Second, create a password for “postgres” PostgreSQL administrative database user/role
switch to the “postgres” system account and create a secure and strong password for PostgreSQL administrative database user/role as follows using psql command.
su - postgres
you will be prompted for password, type the password as set in Step 4.1 above
psql -c "ALTER USER postgres WITH PASSWORD 'secure_password_here';"
if successfull it will display a message
ALTER ROLE
then exit and restart postgresql
exit
sudo systemctl restart postgresql