How to Connect to a PostgreSQL Database From Command Line in Linux

June 18, 2019


PostgreSQL is an open source relational database management system.

Psql is an interactive terminal program for working with PostgreSQL. Use psql to edit, automate, and execute queries in PostgreSQL.

pgAdmin is a web interface for managing PostgreSQL databases. It provides a visual, user-friendly environment with a host of practical solutions that make managing databases easy.

In this tutorial, you will learn how to connect to PostgreSQL from the command line using psql or pgAdmin.

connect to a postgresql database


  • This guide assumes that you have already installed PostgreSQL and created a database you want to access.
  • Access to a command-line/terminal window
  • Sudo or root privileges
  • pgAdmin 4 installed

How to Connect to PostgreSQL Using psql

Installing PostgreSQL creates a default database and user account, both called ‘postgres.’

To log into the ‘postgres’ user account type the following command in the terminal:

sudo -i -u postgres

This example shows the command in a Debian-based distribution, Ubuntu.

Log in to Postgre with single command.

For the same result on a Red Hatbased system, (e.g., Centos and Fedora) use any of the following commands:

su postgres


su -i postgres

These commands open a bash shell and give the user ‘postgres’ root privileges within that shell.

The same command applies if you have already created a different user and a corresponding database with the same name. If a user called ‘test1’, that role will attempt to connect to a database called ‘test1’ by default.

Note: Check out our in-depth article on different ways to create a Postgres user.

To begin using psql, enter the command:


The following screen confirms you are now able to edit and execute queries in PostgreSQL.


PostgreSQL can support and maintain a large number of databases and users simultaneously. Once you log in, it is easy to confirm the current connection and user information.

Simply enter the command:

Command to get information about your postgre connection.

The output helps to determine which user and database you are currently interacting with.

How to Access psql Directly Using sudo

It is possible to connect with PostgreSQL directly and bypass the intermediary bash shell.

If you are sure that all the components of your databases and users are correctly configured, you can log into psql directly:

sudo -i -u postgres psql

The -u (user) option causes sudo to run the specified command as a user other than root. As with the previous method, you can now work with databases by executing queries.

How to Access PostgreSQL With pgAdmin

The pgAdmin is a graphical tool for managing PostgreSQL databases. After installing and configuring the latest browser version of pgAdmin 4, you need to create an initial pgAdmin user account.

The basic setup requires an email and a password to authenticate access to the web interface.

python /usr/lib/python2.7/site-packages/pgadmin4-web/

Once the email and password are defined, access the pgAdmin4 interface by using:




To authenticate, use the email address and password created previously. Once the user interface loads, add a PostgreSQL server by navigating to Servers > Create > Server.

How to access postgre using PGAdmin 3.

The General and Connection tabs allow you to enter values for your server name and database user credentials.

How to create new server with PgAdmin3.

The Hostname/address is the location of the machine where the PostgreSQL server is running. A connection with your user account is established once you save the information entered. The interface presents an overview of the databases that your user account has access to.

To enter and execute queries, click Tools > Query Tool or press ALT+Shift+Q within the current database.

query tool to enter and execute queries


This article provided two (2) simple solutions on how to connect to a PostgreSQL database.

If you are looking for a terminal-based solution, psql is an excellent choice for speed and effectiveness.

The GUI based pgAdmin provides a secure, user-friendly way to log in, administer, and shape databases to fit your requirements. A graphical interface can be indispensable when working on a host of databases simultaneously.

Alternatively, you can also connect to PostgreSQL with SQL Workbench.

Was this article helpful?
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.
Next you should read
How To Install PHP On Ubuntu 20.04 or 22.04
November 24, 2022

PHP is a script-based server-side programming language. PHP is often used to automate server tasks and is the...
Read more
How to Install phpMyAdmin on CentOS 7
October 22, 2018

This guide is for users who have already configured a CentOS server and installed the Apache HTTP services...
Read more
How to Install phpMyAdmin on Ubuntu 18.04
June 18, 2019

The phpMyAdmin tool is a free application for managing a MySQL server. Many users work with Ubuntu Linux...
Read more
How to Install MySQL 8.0 in Ubuntu 18.04
December 12, 2018

MySQL is an open-source relational database server tool for Linux operating systems. It is widely used in...
Read more