How To Install PostgreSQL on Ubuntu 22.04

PostgreSQL Installation on Ubuntu 22.04

PostgreSQL, often referred to as Postgres, is a powerful, open-source object-relational database system. Here's how to install it on Ubuntu 22.04, including information about the default version and steps for installing different versions.

Default Version of PostgreSQL in Ubuntu 22.04

The default version of PostgreSQL in Ubuntu 22.04 is typically the latest stable version available at the time of the Ubuntu release. You can check the specific version in the Ubuntu repository by running apt show postgresql.

Installing PostgreSQL

  1. Update Package Index:
    Open a terminal and update your package list:

    sudo apt update

  2. Install PostgreSQL:
    • To install the default version, use:

      sudo apt install postgresql postgresql-contrib

    • To install a specific version, you need to add the PostgreSQL Apt Repository:
      • Import the repository signing key:

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

      • Add the repository:

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

      • Update the package index:

        sudo apt update

      • Install a specific version of PostgreSQL (replace YOUR_VERSION with the desired version, e.g., 13):

        sudo apt install postgresql-YOUR_VERSION postgresql-contrib-YOUR_VERSION

  3. Starting and Enabling PostgreSQL Service:
    PostgreSQL should be started automatically. To check its status:

    sudo systemctl status postgresql.service

    To ensure it starts on boot:

    sudo systemctl enable postgresql.service

  4. Accessing PostgreSQL:
    By default, PostgreSQL creates a user named postgres. To log in to the PostgreSQL server:

    sudo -u postgres psql

  5. Creating a New Role and Database (Optional):
    • Create a new user (replace newuser with your desired username):

      sqlCopy code

      create user newuser with encrypted password 'password';

    • Create a new database (replace newdb with your desired database name):

      sqlCopy code

      create database newdb owner newuser;

  6. Configuring PostgreSQL (Optional):
    Edit the PostgreSQL configuration file if needed:

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

    Restart PostgreSQL after making changes:

    sudo systemctl restart postgresql

Tips

  • Regularly update your PostgreSQL installation.
  • Secure your database and regularly back up your data.
  • Familiarize yourself with PostgreSQL’s features and best practices for optimization and security.