Use the standard PostgreSQL tool — psql

The psql tool is a standard PostgreSQL client application that you can use to connect to ADPG. It is an interactive terminal that allows you to run SQL queries and view their results. You can execute queries from the command line or from an SQL file. The psql tool is available on each ADPG cluster node.

Run the following commands to start working with psql:

$ sudo su - postgres
$ psql

The first command switches the shell to a login mode with the access rights for the postgres user. The second one runs psql for the default database.

Use the psql options from the table below to specify a database name, host, port, and user instead of default values.

Psql options
Option Description

-d <dbname>

--dbname=<dbname>

Specifies the name of the database to connect to

-h <hostname>

--host=<hostname>

Specifies the host name where the server is located

-p <port>

--port=<port>

Specifies the TCP port on which the server listens for connections. The default value is 5432

-U <username>

--username=<username>

Connects to a database as the specified user

TIP
If you face connection problems, use the pg_isready tool to check the connection status of a PostgreSQL database server.

When the psql command is executed, the terminal displays the psql prompt with the database name and the # character at the end. All subsequent commands should be written after #.

psql (14.3)
Type "help" for help.

postgres=#

You can find examples of SQL queries executed from the command line in the article Example of working with tables via psql.

To run an SQL query from a file, use the meta-command \i:

\i /path/to/file/filename.sql

To logout from psql, use the following command:

\q
Found a mistake? Seleсt text and press Ctrl+Enter to report it