How to Install PostgreSQL on Ubuntu

  • Post category:Ubuntu

PostgreSQL is a powerful, open-source object-relational database system known for its reliability and robust features. This guide will walk you through installing PostgreSQL on Ubuntu 22.04 and similar versions using the default package manager (APT).

Step 1: Update the Package Repository

Before installing PostgreSQL, ensure that your package repository is updated.

Step 1.1: Open Terminal

Press Ctrl + Alt + T to open the terminal.

Step 1.2: Update the Package List

Run the following command to update your package list:

sudo apt update

This ensures that you have the latest version of packages available from the Ubuntu repository.

Step 2: Install PostgreSQL

Now that the package list is updated, you can proceed to install PostgreSQL.

Step 2.1: Install PostgreSQL and its Utilities

Run the following command to install PostgreSQL along with some additional utilities (postgresql-contrib):

sudo apt install postgresql postgresql-contrib

The postgresql-contrib package includes useful add-ons that enhance PostgreSQL’s functionality.

Step 3: Verify PostgreSQL Installation

Step 3.1: Check PostgreSQL Service Status

After installation, the PostgreSQL service should start automatically. To check if the service is running, use this command:

sudo systemctl status postgresql

If the output shows the service as “active (running),” PostgreSQL has been successfully installed.

Step 4: Access PostgreSQL

Step 4.1: Switch to the PostgreSQL User

PostgreSQL creates a system user named postgres during installation. Switch to this user to interact with the database:

sudo -i -u postgres

Step 4.2: Access the PostgreSQL Command Line Interface (CLI)

Once logged in as the postgres user, access the PostgreSQL prompt by running:

psql

You will now be inside the PostgreSQL CLI, where you can execute SQL commands and manage databases.

Step 4.3: Exit the PostgreSQL CLI

To exit the PostgreSQL prompt, type:

\q

Conclusion

By following these steps, you have successfully installed PostgreSQL on your Ubuntu system. Now you can start creating databases, managing users, and setting up your applications. For advanced configurations and management options, be sure to check out PostgreSQL’s official documentation.

For more tutorials and tips, visit CodeAllow, your resource for the latest guides on web development and database management.