How to Install PostgreSQL on Ubuntu 22.04

how to install postgresql on ubuntu 22.04

In this blog post, we are going to show you how to install PostgreSQL on Ubuntu 22.04 OS.

PostgreSQL is a very powerful object-relational database management system written in C and C++ and used for storing and retrieving information. PostgreSQL is a highly compliant and extensible database that can handle multiple tasks at the same time. Learn about the differences between PostgreSQL, MySQL, and SQLite.

Installing PostgreSQL on Ubuntu 22.04 is a very easy task and can take up to 10 minutes. Let’s get started!

Prerequisites

  • A server with Ubuntu 22.04 as OS
  • User privileges: root or non-root user with sudo privileges

Step 1. Update the System

We need to update the system packages to the latest versions available.

sudo apt-get update -y && sudo apt-get upgrade -y

Step 2. Install PostgreSQL

The latest PostgreSQL version 14 is available in the default repository of Ubuntu 22.04, so we just need to execute the command below:

sudo apt install postgresql postgresql-client

Once, installed start and enable the PostgreSQL service.

sudo systemctl enable postgresql.service && sudo systemctl start postgresql.service

Check if the service is up and running:

sudo systemctl status postgresql.service

You should receive the following output:

root@vps:~# sudo systemctl status postgresql.service
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (exited) since Fri 2022-06-17 12:42:57 UTC; 1min 27s ago
   Main PID: 370897 (code=exited, status=0/SUCCESS)
        CPU: 27ms

Jun 17 12:42:56 host.test.vps systemd[1]: Starting PostgreSQL RDBMS...
Jun 17 12:42:57 host.test.vps systemd[1]: Finished PostgreSQL RDBMS.

Another way to check about the PostgreSQL service and port is to run the command below:

sudo netstat -tunlp | grep 5432

You should receive the following output:

root@vps:~# sudo netstat -tunlp | grep 5432
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      370876/postgres

To check the installed PostgreSQL version, execute the following command:

sudo psql --version

You should receive the following output:

root@host:~# sudo psql --version
psql (PostgreSQL) 14.3 (Ubuntu 14.3-0ubuntu0.22.04.1)

Step 3. Configure PostgreSQL

The configuration changes of PostgreSQL 14, are locate at /etc/postgresql/14/main/postgresql.conf on your server. In this file, you can configure incoming connections, security and authentication, resource usage, etc.

In the next few paragraphs, we are going to show you some of the basic PostgreSQL commands and configurations.

Accessing the PostgreSQL console

To access the PostgreSQL console execute the following command on your server:

sudo -u postgres psql

You should receive the following output:

root@vps:~# sudo -u postgres psql
psql (14.3 (Ubuntu 14.3-0ubuntu0.22.04.1))
Type "help" for help.

postgres=#

List Databases

To list the databases, execute the \l command:

You should receive the following output:

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 rows)

postgres=#

Select Database

To select a database, execute the \c “database name” command:

You should receive the following output:

postgres=# \c postgres
You are now connected to database "postgres" as user "postgres".

Quit PostgreSQL shell

To quit from the PostgreSQL shell, just type “\q.”

Allow Remote Access

To allow remote access so PostgreSQL can be accessed from everywhere, you need to open the /etc/postgresql/14/main/pg_hba.conf file and configure the following lines of code to look like those described below:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
host    all             all             0.0.0.0/0                md5

Save the file, close it, and restart the PostgreSQL service for the changes to take effect.

Increase Memory Usage

To increase the memory usage of PostgreSQL open the /etc/postgresql/14/main/postgresql.conf file and configure the following lines of code to look like those described below:

shared_buffers = 256MB

The default value of shared_buffers is 128MB. Save the file, close it and restart the PostgreSQL service for the changes to take effect.

sudo systemctl restart postgresql.service

Congratulations! You successfully installed and configured the latest PostgreSQL 14 version on Ubuntu 22.04.

Of course, you don’t have to install PostgreSQL on Ubuntu 22.04 if you use one of our PostgreSQL VPS Hosting plans, in which case you can simply ask our expert Linux admins to install PostgreSQL 14 on Ubuntu 22.04 for you. They are available 24×7 and will take care of your request immediately.

If you liked this post on how to install PostgreSQL on Ubuntu 22.04, please share it with your friends on social networks or simply leave a reply below.

Leave a Comment