PostgreSQL is a powerful open-source relational database management system known for its robustness and extensive feature set.
To interact with a PostgreSQL database, you can utilize the command-line interface, providing a flexible and efficient way to execute queries and perform administrative tasks.
In this article, we will guide you through the process of logging into a PostgreSQL database from the command line in Linux.
How to Connect to Postgres from Command Line
Before logging into a PostgreSQL database, ensure that you have the necessary client tools installed on your system. These tools include the PostgreSQL command-line client, psql, which allows you to interact with the database from the terminal.
You can install the psql client tool using package managers like apt, yum, or dnf, or by downloading them directly from the PostgreSQL website.
$ sudo apt install postgresql-client OR $ sudo dnf install postgresql
Connect to Local Postgresql Database
To begin, open your terminal and use the psql, a terminal-based front-end to PostgreSQL as follows, where the -d
flag is used to specify the database you are connecting to and the -U
specifies the username you are connecting as.
Note that the following command will allow you to log into a Postgres database running on the localhost:
$ psql -d ubuntumint -U ravi
If the database connection parameters are correct, psql will provide a prompt with the name of the database to which psql is currently connected, followed by the string =>
where you can run SQL commands.
Connect to Remote Postgresql Database
Before you can log into a remote database, you need to know the name of your target database, the hostname or IP address of the machine it is running on, and the port number the Postgres database server is listening on), and what user name you want to connect as.
To access a Postgres database running on a remote host, use the -h
option to specify the host IP address or hostname of the machine on which the server is running, for example:
$ psql -U ravi -h 192.168.0.124 -p 5432 -d ubuntumint
By default, the Postgres database server listens on port 5432 which is why we specified the port number. If your server is listening on a different port, you can use the -p
option to specify the port number (replace 5432 with the actual port number it is listening on).
Press Enter after entering the command, and provide the password to establish a connection to the specified database.
If you get an error message “could not connect to server: Connection refused” indicates that there is an issue establishing a connection to the PostgreSQL server on the specified host and port.
Fix sql: error: could not connect to server: Connection refused
To fix this error, open the configuration file is usually located in the PostgreSQL data directory (e.g., /etc/postgresql/<version>/main/postgresql.conf).
$ sudo nano /etc/postgresql/15/main/postgresql.conf
Look for the following lines:
listen_addresses = '192.168.0.124' # what IP address(es) to listen on; port = 5432 # (change requires restart)
If necessary, update the listen_addresses
and port settings to match your server configuration. Remember to restart the PostgreSQL service after making changes to the configuration file.
$ sudo systemctl restart postgresql
By following these troubleshooting steps, you should be able to diagnose and resolve the “connection refused” error when connecting to the PostgreSQL server.
$ psql -U ravi -h 192.168.0.124 -p 5432 -d ubuntumint
If you still getting an error such as “FATAL: no pg_hba.conf entry for host ‘192.168.0.162’“, then you need to fix it as shown.
Fix FATAL: no pg_hba.conf entry for host ‘192.168.0.162’ Error
To fix this error, open the configuration file is usually located in the PostgreSQL data directory (e.g., /etc/postgresql/<version>/main/pg_hba.conf).
$ sudo nano /etc/postgresql/15/main/pg_hba.conf
Inside the pg_hba.conf file, you need to add an entry that allows the specified host, user, and database to connect.
host ubuntumint ravi 192.168.0.162/32 md5
Make sure to replace ubuntumint with the correct database name, ravi with the correct username, and 192.168.0.162 with the actual IP address of the client machine.
After making the necessary changes, you need to restart the PostgreSQL service:
$ sudo systemctl restart postgresql
Now attempt to connect to the PostgreSQL server again using the specified host, user, and database, it should allow the connection.
$ psql -U ravi -h 192.168.0.124 -p 5432 -d ubuntumint
Once logged in, you can start executing queries and performing administrative tasks on the PostgreSQL database directly from the command line. The psql client provides a comprehensive set of commands and features to manage and query the database.
Conclusion
Logging into a PostgreSQL database from the command line using the psql client provides a convenient and efficient way to interact with the database.
By following the steps outlined in this article, you can establish a connection to a PostgreSQL database, execute queries, and perform administrative tasks directly from your terminal.
Mastering the command-line interface allows you to harness the full potential of PostgreSQL’s capabilities and efficiently manage your data.