How to Install PostgreSQL on Ubuntu

PostgreSQL, also known as Postgres is a powerful open-source object-relational database system with a strong reputation for reliability, feature robustness, and performance. Installing PostgreSQL on an Ubuntu system is a bit complex you must be familiar with the Linux command line.

This guide will walk you through the process of install PostgreSQL Ubuntu system, providing a step-by-step approach for both beginners and experienced users.

Understanding PostgreSQL Versions in Ubuntu

Ubuntu repositories include a specific PostgreSQL version that aligns with the Ubuntu release cycle and receives security updates throughout its lifetime. However, if you require a specific version not included in the default repositories, you can leverage the official PostgreSQL APT repository. This repository offers access to all supported PostgreSQL versions and integrates seamlessly with your system’s package management for automatic updates.

Getting Started

Before diving into the installation, ensure you have the following:

  1. An Ubuntu System: This guide is for Ubuntu users. You can check the Ubuntu version for compatibility.
  2. Superuser Privileges: Many installation steps require administrative access. You can use sudo before commands to grant temporary superuser privileges.

Installation Methods PostgreSQL on Ubuntu

There are two primary methods to configure PostgreSQL Ubuntu Linux operating system:

Method 1: Using Default Ubuntu Repositories

  • Update the package list

You can update the package list by simply typing the following command:

sudo apt-get update
update command
  • Install PostgreSQL Packages:

Install the core PostgreSQL package and the postgresql-contrib package for additional utilities:

sudo apt install postgresql postgresql-contrib

This command retrieves and installs the necessary packages from the official Ubuntu repositories.

postgre-sql contrib

After installation, you can verify it by checking the PostgreSQL version with the following command:

psql --version
psql  version


Method 2: Using the Official PostgreSQL APT Repository

  • Add the PostgreSQL APT Repository

To install latest PostgreSQL Ubuntu, identify the appropriate repository list based on your Ubuntu version by visiting the official PostgreSQL download page for Ubuntu. Locate the package list URL corresponding to your Ubuntu version and copy it. Then use the curl command to download the repository list and add it to your system’s sources.

curl -sSL [repository_list_URL] | sudo tee /etc/apt/sources.list.d/postgresql.list

Replace repository_list_URL with the copied URL.

  • Import the Repository GPG Key

The repository uses a GPG key to verify the authenticity of packages. Import the key using:

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4C85.asc | sudo apt-key add -
  • Update Package Lists

Refresh the package lists to include the newly added repository:

sudo apt update
  • Install PostgreSQL Packages

Similar to Method 1 install PostgreSQL with the desired version by specifying the version number after the package name:

sudo apt install postgresql-15 && sudo apt install postgresql-contrib-15

Replace 15 with your desired version and install contrib package for the specific version.

Post Installation

Once the installation is complete, perform the following setup PostgreSQL on Ubuntu:

1. Initialize the PostgreSQL Cluster

The initial cluster needs to be initialized after the first installation. This process creates essential PostgreSQL directories and structures. Run the following command:

sudo su - postgres -c initdb -D /var/lib/postgresql/[version]

Replace [version] with your installed version. For example, 15 as mentioned above.

2. Set the Default Authentication Method

PostgreSQL uses peer authentication, which allows local connections without a password. This is not recommended for production environments. To configure password authentication edit the pg_hba.conf file:

sudo nano /etc/postgresql/[version]/main/pg_hba.conf

Locate the line mentioning local all all peer and modify it to local all all trust for password-based local connections or local all all md5 for MD5 password authentication. Save the changes and restart the service.

3. Starting PostgreSQL service

Simply type the following command to start the PostgreSQL service:

sudo systemctl start postgresql

4. Check Service Status

Type the following command to check the status of the PostgreSQL service:

sudo systemctl status postgresql

This command should display that the service is “active (running)”.

postgresql status

5. Connect to PostgreSQL Shell

Use the psql command to connect to the PostgreSQL shell as the default user “postgres”:

sudo su - postgres -c psql

If the connection is successful, you’ll see the PostgreSQL prompt.

postgresql shell


6. Exit PostgreSQL Shell

To exit the PostgreSQL shell, type \q and press Enter. Further, you can explore PostgreSQL databases create new users, manage users, and perform various database administration tasks.

Conclusion

By following these steps you have successfully installed and configured PostgreSQL on your Ubuntu system. Remember to prioritize security by setting strong passwords and implementing proper access controls. With this guide, you can start database management in PostgreSQL according to your requirements.

Installing PostgreSQL on Ubuntu can be done but managing complex database workloads on a regular hosting plan might not be ideal. Upgrading to an Ultahost Linux VPS server empowers you with greater control and scalability which grants you root access allowing you to channel PostgreSQL configuration for optimal performance.

FAQ

What is PostgreSQL?
Is PostgreSQL free to use?
Can I install PostgreSQL on Ubuntu?
Do I need technical knowledge to install PostgreSQL on Ubuntu?
How long does it take to install PostgreSQL on Ubuntu?

Related Post

How to Import a CSV file into a MySQL databas

Importing a structured CSV file into your MySQL databas...

How to Install phpPgAdmin on Windows 10

phpPgAdmin is a free, web-based interface for managing ...

How to Install Java on Ubuntu 22.04

Java is a versatile programming language widely used fo...

How to Install UFW on Ubuntu 22.04

In Ubuntu 22.04, UFW stands for Uncomplicated Firewall....

How to Install Magento on Ubuntu

Magento is a powerful open-source e-commerce platform t...

How to Install OpenJDK on Ubuntu

OpenJDK the Open Java Development Kit is a free and ope...

Leave a Comment