How to Install PostgreSQL on Ubuntu 22.04: A Step-by-Step Guide

Tutorial:

Step 1: Update Your System

Update the package lists to ensure you have the latest versions of packages:

sudo apt update && sudo apt upgrade -y

Step 2: Install PostgreSQL

Ubuntu includes PostgreSQL in its default repositories. Install it using:

sudo apt install postgresql postgresql-contrib -y

Step 3: Start and Enable PostgreSQL Service

Make sure the PostgreSQL service is running and enable it to start on boot:

sudo systemctl start postgresql
sudo systemctl enable postgresql

Step 4: Switch to the PostgreSQL User

PostgreSQL creates a default user named postgres. Switch to this user:

sudo -i -u postgres

Step 5: Access the PostgreSQL Command Line

Enter the PostgreSQL interactive terminal:

psql

Step 6: Create a New Database and User

In the PostgreSQL prompt, you can create a new database and user:

CREATE DATABASE my_database;
CREATE USER my_user WITH PASSWORD 'secure_password';
GRANT ALL PRIVILEGES ON DATABASE my_database TO my_user;

Exit the prompt with:

\q

Step 7: Allow Remote Connections (Optional)

If you want PostgreSQL to accept remote connections:

  1. Edit the PostgreSQL configuration file:
    sudo nano /etc/postgresql/14/main/postgresql.conf

    Find and update the listen_addresses line to:

    listen_addresses = 'localhost, your_ip'
  2. Configure the client authentication:
    sudo nano /etc/postgresql/14/main/pg_hba.conf

    Add the following line:

    host all all your_ip/32 md5
  3. Restart PostgreSQL:
    sudo systemctl restart postgresql

Step 8: Test the Installation

Check the PostgreSQL version to confirm successful installation:

psql --version

You’re all set to start using PostgreSQL on Ubuntu 22.04!

Download Now No episodes found.Posted in Database, UbuntuTagged , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *