How to Install and Configure Postgres 14 Ubuntu 22.04

Postgresql is an open source object-relational database system with over 30 years of active development that has earned it a strong reputation for reliability, feature robustness, and performance. It is a free and open-source relational database management system emphasizing extensibility and SQL compliance.

It was originally named POSTGRES, referring to its origins as a successor to the Ingres database developed at the University of California, Berkeley. PostgreSQL is used as the primary data store or data warehouse for many web, mobile, geospatial, and analytics applications. PostgreSQL can store structured and unstructured data in a single product.

In this guide we are going to set up and install Postgresql 14 in Ubuntu 22.04.

Related content:

# Prerequisites

To follow along, ensure you have the following:

  1. Ubuntu 20.04 server
  2. Root access to the server or user with root access
  3. Internet access from the server
  4. Knowledge of Linux terminal

# Ensure that your system packages are up to date

Let’s refresh your server’s local package index using this command:

1
sudo apt update

Then we upgrade the packages in our system with this:

1
sudo apt -y upgrade

# Install Postgres 14 in Ubuntu

Ubuntu 22.04 might not have the latest version of Postgres configured. In this guide we will set up Postgres repository in our server then use it to install postgres.

To add the repository use this command:

1
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

Then import the repository signing key:

1
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

Now let’s update the package lists:

1
sudo apt update

Then we install the specific version of postgres we want. Use postgresql-14 instead of postgresql:

1
sudo apt -y install postgresql-14

Once the installation is successful, Postgres 14 will be started.

Use this command to check the service status:

1
2
3
4
5
6
7
8
9
$ sudo systemctl status postgresql
ā— postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; preset: enabled)
     Active: active (exited) since Fri 2023-10-20 13:06:43 UTC; 12s ago
   Main PID: 66818 (code=exited, status=0/SUCCESS)
        CPU: 2ms

Oct 20 13:06:43 fiddle-etestdb0.c.upvest-fiddle.internal systemd[1]: Starting postgresql.service - PostgreSQL RDBMS...
Oct 20 13:06:43 fiddle-etestdb0.c.upvest-fiddle.internal systemd[1]: Finished postgresql.service - PostgreSQL RDBMS.

Next, let’s erify the installation by connecting to the PostgreSQL database server and checking its version. Use this command:

1
sudo -u postgres psql -c "SELECT version();"

Output:

1
2
3
4
5
$ sudo -u postgres psql -c "SELECT version();"
                                                           version
------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 14.3 (Ubuntu 14.3-1.pgdg22.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 11.2.0-19ubuntu1) 11.2.0, 64-bit
(1 row)

# PostgreSQL Roles and Databases Authentication Methods

PostgreSQL uses a concept called roles to handle client authentication and authorization. By default, Postgres is set up to use ident authentication, meaning that it associates Postgres roles with a matching Unix/Linux system account. If a role exists within Postgres, a Unix/Linux username with the same name is able to sign in as that role.

The installation procedure created a user account called postgres that is associated with the default postgres role. In order to use PostgreSQL, you can log in to that account.

PostgreSQL supports multiple authentication methods . The most commonly-used methods are:

  • Trust - A role can connect without a password, as long as the conditions defined in the pg_hba.conf are met.
  • Password - A role can connect by providing a password. The passwords can be stored as scram-sha-256, md5, and password (clear-text).
  • Ident - Only supported on TCP/IP connections. It works by obtaining the client’s operating system user name, with an optional user name mapping.
  • Peer - Same as Ident, but it is supported on local connections only.

# Connecting to postgres database

# By Switching to postres user

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

1
sudo -i -u postgres

You can now access a Postgres prompt immediately by typing:

1
2
3
4
5
$ psql
psql (14.3 (Ubuntu 14.3-1.pgdg22.04+1))
Type "help" for help.

postgres=#

This will log you into the PostgreSQL prompt, and from here you are free to interact with the database management system right away.

# By running the command as postgres user

Use this to run the command directly as the postgres user using sudo

1
2
3
4
5
$ sudo -u postgres psql
psql (14.3 (Ubuntu 14.3-1.pgdg22.04+1))
Type "help" for help.

postgres=#

# Configuring postgres instance for remote access

By default postgres is only set up to be accessed locally. This is not ideal if we want to access our server from another host. In this section we are going to configure postgres to allow access from remote hosts.

Postgres 14 configuration file can be found in this path/etc/postgresql/14/main/pg_hba.conf

1
2
$ file /etc/postgresql/14/main/pg_hba.conf
/etc/postgresql/14/main/pg_hba.conf: ASCII text

# Change peer identification to trust

Use this command to change the peer to trust

1
sed -i '/^local/s/peer/trust/' /etc/postgresql/14/main/pg_hba.conf

This will update the line in the config file to this:

1
2
# "local" is for Unix domain socket connections only
local   all             all                                     trust

# Add a block to allow access from everywhere

Add this content to the file /etc/postgresql/14/main/pg_hba.conf to allow all hosts password access:

1
vim /etc/postgresql/14/main/pg_hba.conf

Then add this:

1
host    all             all             0.0.0.0/0                md5

# Ensure PostgreSQL is listening on all hosts (*)

Add this line to the config here /etc/postgresql/14/main/postgresql.conf to allow postgres to listen on all hosts

1
sudo vim /etc/postgresql/14/main/postgresql.conf

Then add this line:

1
listen_addresses='*'

To apply the configurations, we need to restart the postgres 14 service.

Enable and restart postgresql server to reload the configs:

1
2
sudo systemctl restart postgresql
sudo systemctl enable postgresql

# User management

# Create Super user:

It would be better if we created a super user to administer the postgres service. This is that one user that has permissions to manage other users and databases.

Connect to the DB as postres role

1
2
3
4
5
6
$ sudo -u postgres psql

psql (14.3 (Ubuntu 14.3-1.pgdg22.04+1))
Type "help" for help.

postgres=#

Create super user with name root using this command:

1
CREATE ROLE root WITH LOGIN SUPERUSER CREATEDB CREATEROLE PASSWORD 'passwordhere';

Check the user if its been created and granted necessary privileges:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
postgres=# CREATE ROLE root WITH LOGIN SUPERUSER CREATEDB CREATEROLE PASSWORD 'passwordhere';
CREATE ROLE
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 root      | Superuser, Create role, Create DB                          | {}

postgres=#

# Managing application users

Use this to create a database, create a user and grant that user all accesss to that database:

1
2
3
create database app_db_name;
create user app_user with encrypted password 'dbpassword';
grant all privileges on database app_db_name to app_user;

For more information on user permission management, please checkout this comprehensive guide on user and permission management in postgres here.

# Connecting to the instance from remote host

Use this command to test that you can connect:

1
2
3
4
psql 'postgres://<username>:<password>@<host>:<port>/<db>?sslmode=disable'

# example
psql 'postgres://root:passwordhere@192.160.1.20:5432/postgres?sslmode=disable'

# Conclusion

Up to this point we have managed to install Postgresql 14 on an Ubuntu server, do some basic configurations then do basic user management.

Last updated on Mar 20, 2024 17:19 +0300
comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy