How To Connect PostgreSQL Database In Linux

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.

how to connect postgresql database in linux

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.

how to connect to database in postgresql in linux

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.

how to connect to postgresql database in linux

Step 4: List Databases

Once you’re in the PostgreSQL shell, get the list of available databases by executing the following command.

\l
how to connect to remote postgresql database in linux

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.

Connect PostgreSQL Database In Linux

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.

how to connect remote postgresql database in linux

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:

Top 200 SQL Server Interview Questions and Answers

Free PDF On Top 200 SQL Server Interview Questions And Answers

Download A 40 pages PDF And Learn Now.