Easy Steps To Install PostgreSQL on Ubuntu 22.04 – OrcaCore

Posted on

Easy Steps To Install PostgreSQL on Ubuntu 22.04 – OrcaCore

This guide will walk you through the process of How To Install PostgreSQL on Ubuntu 22.04. PostgreSQL stands as a robust, open-source object-relational database system. With over 15 years of active development, its proven architecture has solidified a strong reputation for reliability, data integrity, and accuracy. Follow the steps below to install PostgreSQL 14, which comes as the default version on Ubuntu 22.04. The Easy Steps To Install PostgreSQL on Ubuntu 22.04 described here are simple to follow.

Before starting this guide on PostgreSQL 14 on Ubuntu 22.04, ensure you are logged into your server as a non-root user with sudo privileges. For assistance with this, refer to the Initial Server Setup with Ubuntu 22.04 guide.

1. Install PostgreSQL 14 Ubuntu 22.04

The PostgreSQL 14 packages are conveniently available in the default Ubuntu 22.04 repository.

First, update your local package index using the following command:

sudo apt update

Next, install PostgreSQL and its necessary dependencies by running this command:

sudo apt install postgresql postgresql-contrib

Once the installation is complete, let’s explore how to use PostgreSQL.

2. How to Use PostgreSQL 14 Ubuntu 22.04

PostgreSQL employs a system of roles for managing authentication and authorization. During the installation process, PostgreSQL is configured to use ident authentication. This associates PostgreSQL roles with a corresponding Unix/Linux system account.

If a role exists within PostgreSQL, a Unix/Linux username bearing the same name can log in as that role.

One method to interact with PostgreSQL is to switch to the Postgres account on your server using this command:

sudo -i -u postgres
**Output**
postgres@sam:~$

Access the Postgres shell on Ubuntu 22.04 by executing the following command:

postgres@sam:~$ psql

Upon successful execution, your shell prompt will change to:

access the Postgres shell on Ubuntu 22.04

To exit the PostgreSQL shell, use the following command:

postgres=# q

This will return you to the Postgres Linux command prompt:

postgres@sam:~$

Type exit to return to your regular system user.

postgres@sam:~$ exit

Alternatively, you can connect directly to the Postgres shell on Ubuntu 22.04 using:

sudo -u postgres psql

This command bypasses the need to switch users, directly connecting you to the PostgreSQL shell.

Create a new PostgreSQL Role

There are two primary ways to create a new Postgres role.

First, if you’re logged in as the Postgres account, use:

postgres@sam:~$ createuser --interactive

Second, use sudo with each command from your normal Ubuntu 22.04 account:

sudo -u postgres createuser --interactive

Both methods prompt you to enter the role name and specify whether the new role should be a superuser.

**Output**
Enter name of role to add: orca
Shall the new role be a superuser? (y/n) y

Create a new PostgreSQL Database

The Postgres authentication system requires that any role used for login must have a database with the same name for access.

If logged in as the Postgres account, create the database matching the role name created earlier:

postgres@sam:~$ createdb orca

Or, use sudo to create the database from your normal account:

sudo -u postgres createdb orca

Open the Postgres Shell with the new Role

Create a Linux user with the same name as your Postgres role and database. Use the following command:

sudo adduser orca

Now, connect to the Postgres database with these commands:

# sudo -i -u orca
# orca@sam:~$ psql

Alternatively, use:

sudo -u orca psql

Once logged into your database, verify your connection details with:

orca=# conninfo

The output will display:

**Output**
You are connected to database "orca" as user "orca" via socket in "/var/run/postgres

For further information, consult the PostgreSQL Documentation page.

Conclusion

You’ve now successfully learned How To Install PostgreSQL on Ubuntu 22.04 and its basic usage. Ubuntu 22.04 defaults to PostgreSQL 14, making it easy to install and access.

Hope you enjoy it.

How To Install and Use Iptables on Ubuntu 22.04

Set Up Time Synchronization on Ubuntu 22.04

Enable and Configure SSH on Ubuntu 22.04

Alternative Solutions for Installing PostgreSQL on Ubuntu 22.04

While the apt package manager provides a straightforward method for installing PostgreSQL, there are alternative approaches that offer different levels of control and flexibility. Here are two such alternatives: using Docker and building from source. These are alternate Easy Steps To Install PostgreSQL on Ubuntu 22.04.

1. Installing PostgreSQL using Docker

Docker allows you to run PostgreSQL in a container, providing isolation from the host system and simplifying dependency management. This is especially useful when you need a specific PostgreSQL version or want to avoid conflicts with other software on your server.

Explanation:

Docker containers encapsulate an application and its dependencies into a single package. This ensures that the application runs consistently across different environments. When installing PostgreSQL with Docker, you download a pre-built PostgreSQL image from Docker Hub, which contains everything needed to run the database server.

Steps:

  1. Install Docker: If Docker isn’t already installed on your Ubuntu 22.04 system, you can install it with:
sudo apt update
sudo apt install docker.io
sudo systemctl start docker
sudo systemctl enable docker
  1. Pull the PostgreSQL Docker Image: Obtain the official PostgreSQL image from Docker Hub. You can specify a version tag (e.g., 14) or use latest for the most recent version.
sudo docker pull postgres:14
  1. Run the PostgreSQL Container: Start a new container based on the pulled image. You’ll need to set an environment variable for the POSTGRES_PASSWORD (which is mandatory) and map a host port to the container’s PostgreSQL port (5432). You might also want to mount a volume to persist data outside the container.
sudo docker run --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -v postgres_data:/var/lib/postgresql/data -d postgres:14
*   `--name my-postgres`: Assigns a name to the container.
*   `-e POSTGRES_PASSWORD=mysecretpassword`: Sets the PostgreSQL superuser password. Replace `mysecretpassword` with a strong password.
*   `-p 5432:5432`: Maps port 5432 on the host to port 5432 in the container.
*   `-v postgres_data:/var/lib/postgresql/data`: Mounts a named volume `postgres_data` to persist data.  This prevents data loss when the container is stopped or removed.
*   `-d postgres:14`: Runs the container in detached mode (in the background).
  1. Access PostgreSQL: You can now access the PostgreSQL server running inside the Docker container using psql or any other PostgreSQL client. To connect from the host machine, use localhost or 127.0.0.1 as the hostname. First, you will have to enter the running container:
sudo docker exec -it my-postgres bash

Then, connect to PostgreSQL using the psql command:

psql -U postgres -h localhost

You might need to install the client tools inside the container if they are not present in the default image.

2. Building PostgreSQL from Source

Building PostgreSQL from source gives you the ultimate control over the installation process. This allows you to customize compile-time options, optimize for your specific hardware, and use the very latest development version.

Explanation:

Building from source involves downloading the PostgreSQL source code, configuring the build process, compiling the code, and installing the resulting binaries. This process requires more technical expertise but allows for fine-grained control.

Steps:

  1. Install Build Dependencies: Before you can build PostgreSQL, you need to install the necessary build tools and libraries:
sudo apt update
sudo apt install build-essential libreadline-dev zlib1g-dev flex bison libssl-dev libxml2-dev
  1. Download the Source Code: Download the PostgreSQL source code from the official PostgreSQL website or a mirror. You can use wget to download the tarball:
wget https://ftp.postgresql.org/pub/source/v14.8/postgresql-14.8.tar.gz
Replace `v14.8` with the desired version.
  1. Extract the Source Code: Extract the downloaded tarball:
tar -xzf postgresql-14.8.tar.gz
cd postgresql-14.8
  1. Configure the Build: Use the configure script to set up the build environment. You can specify various options, such as the installation directory:
./configure --prefix=/usr/local/pgsql14
This will install PostgreSQL to `/usr/local/pgsql14`.  You can adjust this to your preferred location.
  1. Build and Install: Compile the source code and install the binaries:
make
sudo make install
  1. Set Up Environment Variables: Add the PostgreSQL binaries to your PATH environment variable. Edit your ~/.bashrc or ~/.zshrc file and add the following lines:
export PGHOME=/usr/local/pgsql14
export PATH=$PGHOME/bin:$PATH
export MANPATH=$PGHOME/share/man:$MANPATH
Then, source the file:
source ~/.bashrc
  1. Initialize the Database Cluster: Create a data directory and initialize the database cluster:
mkdir /usr/local/pgsql14/data
sudo chown postgres:postgres /usr/local/pgsql14/data
sudo -u postgres /usr/local/pgsql14/bin/initdb -D /usr/local/pgsql14/data
  1. Start the PostgreSQL Server: Start the PostgreSQL server:
sudo -u postgres /usr/local/pgsql14/bin/pg_ctl -D /usr/local/pgsql14/data -l logfile start
To stop the server:
sudo -u postgres /usr/local/pgsql14/bin/pg_ctl -D /usr/local/pgsql14/data stop

Building from source provides maximum control and customization but requires a more in-depth understanding of the build process and PostgreSQL internals.

These alternative methods provide different approaches to installing and managing PostgreSQL on Ubuntu 22.04, catering to varying needs and levels of expertise. The Easy Steps To Install PostgreSQL on Ubuntu 22.04 are now presented with flexibility.