Install PostgreSQL on Ubuntu 22.04

Last Updated on July 16, 2022 by Pupli

# Create the file repository configuration:
sudo sh -c 'echo "deb $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

# Import the repository signing key:
wget --quiet -O - | sudo apt-key add -

# Update the package lists:
sudo apt-get update

# Install the latest version of PostgreSQL.
# If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql':
sudo apt-get -y install postgresql postgresql-contrib
sudo systemctl start postgresql.service

Update PostgreSQL admin user’s password

The postgresql database admin user is created with the installation of PostgreSQL database server. We need to set a secure password for this user.

sudo su - postgres
psql -c "alter user postgres with password 'MySt0ngDBP@ss'"

Switching Over to the postgres Account

Switch over to the postgres account on your server by typing:

sudo -i -u postgres

You can now access the PostgreSQL prompt immediately by typing:


From there you are free to interact with the database management system as necessary.
Exit out of the PostgreSQL prompt by typing:


This will bring you back to the postgres Linux user’s command prompt.

Accessing a Postgres Prompt Without Switching Accounts

sudo -u postgres psql