Connecting to PostgreSQL Database via Linux Shell Command

Posted on

Connecting to PostgreSQL Database via Linux  Shell Command

Operating System            : Linux Ubuntu Xenial Xerus (16.04)

Database                             : PostgreSQL 9.5.3

To be able to connect to PostgreSQL server database, we have to make sure that the service which is provided is active.  After reassuring that the service is active, we can use a command line or terminal which exists in the operating system to connect to PostgreSQL server database.

Make sure that we are connecting to the right port where the default port which is used to listen incoming request to access or connect to PostgreSQL server database is port 5432.

Type the following command in the command prompt :


If we execute the above command, we may succeed or even getting a warning message as follows :

psql: FATAL: role “username” does not exist

In the above error message, username in this context is the user you are currently logging in. You can check who you are logging in as by typing the following command ‘whoami’, or just try to read the following article in this link.

Normally, we can use the role which is already exists after successfully installing PostgreSQL database server. One of that role is ‘postgres’, so we can try to log in again by using the role ‘postgres’ as follows :

psql –Upostgres

This is the real output condition when we type the command :

psql –Upostgres
Password for user postgres:
psql (9.5.3, server 9.3.10)
Type “help” for help

Just type the correct password when we are given the prompt for Password for user postgres as shown above. In a seconds, we will be able to enter and use the PostgreSQL command console.

So, for a quick recap on connecting to PostgreSQL via command prompt, you have to do the following things before :

  1. Off course you need to have a PostgreSQL database installed.
  2. Make sure that you have start the service.
  3. You need to know the port number of PostgreSQL service is running. By default , it runs in port 5432, so we assumed that it runs in that port.
  4. Use the correct role and password to connect to PostgreSQL database server and enter the PostgreSQL command console.



One thought on “Connecting to PostgreSQL Database via Linux Shell Command

Leave a Reply