How to Install PostgreSQL on Ubuntu 22.04

PostgreSQL is a free and open-source relational database management system (ORDBMS) emphasizing extensibility and SQL compliance. It is known for its strong reputation for reliability, data integrity, and performance.

PostgreSQL is available for Linux, Windows, FreeBSD, OpenBSD, and macOS.

In this post, we will see how to install PostgreSQL on Ubuntu 22.04.

Install PostgreSQL on Ubuntu 22.04

You can install the PostgreSQL database server from,

  1. Ubuntu repository
  2. PostgreSQL repository

1. Install PostgreSQL from Ubuntu Repository

Installing PostgreSQL from the Ubuntu repository is a straightforward method. Hardly it takes 5 minutes to install PostgreSQL.

First, update the repository index.

sudo apt update

Then, install the latest version of PostgreSQL (v14) using the below command.

sudo apt install -y postgresql

By now, the PostgreSQL service will be up and running. You can check the status of the service with the below command.

sudo systemctl status postgresql

Output:

 postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (exited) since Wed 2022-05-25 01:39:45 EDT; 2min 5s ago
    Process: 4794 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 4794 (code=exited, status=0/SUCCESS)
        CPU: 1ms

May 25 01:39:45 ubuntu2204 systemd[1]: Starting PostgreSQL RDBMS...
May 25 01:39:45 ubuntu2204 systemd[1]: Finished PostgreSQL RDBMS.

2. Install PostgreSQL from PostreSQL Repository

PostgreSQL Global Development Group publishes packages for Ubuntu operating system through dedicated repositories, and the packages in their repository are fresher than those available in the OS repository.

First, add the PostgreSQL repository to your system with the below command.

wget -qO - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo gpg --dearmor -o /usr/share/keyrings/postgresql-keyring.gpg

echo "deb [arch=amd64 signed-by=/usr/share/keyrings/postgresql-keyring.gpg] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/postgresql.list

Then, update the repository index.

sudo apt update

Finally, install the latest version of PostgreSQL using the below command.

sudo apt install -y postgresql

By now, the PostgreSQL service should be up and running.

Change PostgreSQL Server Listening Address

By default, PostgreSQL listens on the localhost (127.0.0.1). This behavior restricts external applications from connecting to the database. So, you need to configure PostgreSQL to listen to the system IP address to allow connections from external applications. To do that, edit the postgresql.conf file.

sudo nano /etc/postgresql/14/main/postgresql.conf

Then, set the listen_addresses to * or system <IPAddress>.

listen_addresses = '192.168.0.10'

Finally, restart the PostgreSQL service.

sudo systemctl restart postgresql

Access PostgreSQL Database Server

To manage the PostgreSQL database, you will need to log in as a postgres user (Linux user) and have to invoke PSQL shell using the psql command.

sudo -u postgres psql

Output:

psql (14.3 (Ubuntu 14.3-0ubuntu0.22.04.1))
Type "help" for help.

postgres=#

On psql shell, run the below command to change the postgres user (Database admin password).

postgres=# \password

OR

postgres=# \password postgres

Conclusion

That’s All. I hope you have learned how to install PostgreSQL on Ubuntu 22.04.

databasepostgresqlubuntu 22.04
Comments (0)
Add Comment