In this PostgreSQL tutorial, we will learn how to connect to a PostgreSQL database in Linux. If you’re using Linux as an operating system and have PostgreSQL installed, this step-by-step tutorial will take you through the process of connecting to a PostgreSQL database.
How To Connect PostgreSQL Database In Linux
To connect PostgreSQL database in Linux, follow the steps below
- Step 1: Start PostgreSQL Service
- Step 2: Switch to PostgreSQL User
- Step 3: Get access to the PostgreSQL Shell (psql)
- Step 4: List Databases
- Step 5: Connect to a Database
- Step 6: Verify the Connection
Step 1: Start PostgreSQL Service
To begin, if you have already installed PostgreSQL, start the PostgreSQL service by executing the following command in the Linux terminal.
sudo service postgresql start
Check out the screenshot below for the output.

Step 2: Switch to PostgreSQL User
Switch to the PostgreSQL user account in order to connect to the database by executing the following command.
sudo -i -u postgres
Check out the screenshot below for your reference.

Step 3: Get access to the PostgreSQL Shell (psql)
After switching to the PostgreSQL user, access the PostgreSQL shell using the following command.
psql
Check out the screenshot below.

Step 4: List Databases
Once you’re in the PostgreSQL shell, get the list of available databases by executing the following command.
\l

Step 5: Connect to a Database
Use the following command syntax to connect to your database.
\c <databasename>
Replace <databasename> with the name of the database you want to connect to. For example, from the list of available databases, connect to the “postgres” database using the below command.
\c postgres
You’ll need to enter your username and password if the database you’re connected to needs authentication. To create the connection, provide the needed credentials.
After executing the above command, you see the message on the terminal “You are now connected to database ‘postgres’ as user ‘postgres’ “, as shown in the picture below.

Step 6: Verify the Connection
You can run SQL queries and interact with the database after successfully connecting to the PostgreSQL database. To check the connection, run the below query.
SELECT version();
Check out the screenshot below, which shows the expected output.

The above query displays the version of PostgreSQL you’re connected to.
Conclusion
In this PostgreSQL tutorial, we have learned how to connect to the PostgreSQL database on the Linux system. By following the step-by-step tutorial, you should be able to connect to a PostgreSQL database, perform SQL queries, and interact with the database using the PostgreSQL shell (psql).
You may like to read:
- How to use PostgreSQL To_Number
- How to Download and Install PostgreSQL on Windows
- PostgreSQL Update Limit
- How to create user in PostgreSQL

Rajkishore Sahoo is a Microsoft Azure consultant with more than 14 years of hands-on experience building cloud-native data solutions across Azure and AWS, focused on Azure SQL Database, Azure SQL Managed Instance, and the pipelines that connect them to production apps. Day to day, he provisions Azure SQL instances, configures firewalls and connection strings, tunes queries for cloud workloads, and wires databases into Azure Functions and Logic Apps. Read more