DEV Community

Mezbah Alam
Mezbah Alam

Posted on

Installing and Configuring latest PostgreSQL on Ubuntu 22.04

To install PostgreSQL on Ubuntu 22.04, follow these steps:
Update the package manager's package list by running the following command:

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Install the PostgreSQL package by running the following command:

sudo apt install postgresql
Enter fullscreen mode Exit fullscreen mode

After the installation is complete, start the PostgreSQL service by running the following command:

sudo systemctl start postgresql
Enter fullscreen mode Exit fullscreen mode

To make sure that the PostgreSQL service starts automatically when the system boots, run the following command:

sudo systemctl enable postgresql
Enter fullscreen mode Exit fullscreen mode

To check the version of PostgreSQL installed on your system, run the following command:
For Server version:

pg_config --version
Enter fullscreen mode Exit fullscreen mode

For Client version:

psql --version
Enter fullscreen mode Exit fullscreen mode

To log in to the PostgreSQL database as the default "postgres" user, run the following command:

sudo -u postgres psql
Enter fullscreen mode Exit fullscreen mode

This will open the PostgreSQL interactive terminal. From here, you can create databases, users, and perform other tasks. For example, to create a new database called "mydb_name", run the following command:

postgres=# create database mydb_name;
Enter fullscreen mode Exit fullscreen mode

To create a new user called "myuser" with the password "mypass", run the following command:

postgres=# create user myuser with encrypted password 'mypass';
Enter fullscreen mode Exit fullscreen mode

To grant all privileges on the "mydb_name" database to the "myuser" user, run the following command:

postgres=# grant all privileges on database mydb_name to myuser;
Enter fullscreen mode Exit fullscreen mode

If you want to log in as a different user, you can use the following command:

psql -U username
Enter fullscreen mode Exit fullscreen mode

Replace "username" with the name of the user you want to log in as.
That's it! You have successfully installed and configured PostgreSQL on Ubuntu 22.04. You can now use the interactive terminal or connect to the database from your application to perform various tasks.

Top comments (0)