How to Connect to PostgreSQL using psql

How to Connect to PostgreSQL using psql: Different Ways

Table of Contents



1. pg_hba.conf Line

The way you write the psql command (with or without -h, and what you write after it) decides which line in pg_hba.conf is used. PostgreSQL reads the file from top to bottom and uses the first line that matches.

[postgres@pglab01 ~]$ cat /pgdata/pgsql-17/data/pg_hba.conf
# TYPE     DATABASE   USER      ADDRESS         METHOD           OPTIONS
local      all        all                       scram-sha-256     <---- 1. Unix socket (psql without -h)
host       all        all       127.0.0.1/32    scram-sha-256     <---- 2. TCP IPv4 (-h 127.0.0.1)
host       all        all       ::1/128         scram-sha-256     <---- 3. TCP IPv6 (-h ::1, usually also -h localhost)
host       all        all       192.168.2.0/24  scram-sha-256     <---- 4. TCP from other hosts (-h 192.168.2.101)

[postgres@pglab01 ~]$
psql command Connection type pg_hba.conf line used .pgpass host field
psql -U postgres -d postgres Unix socket local all all scram-sha-256 localhost
psql -h 127.0.0.1 -U postgres -d postgres TCP IPv4 host all all 127.0.0.1/32 scram-sha-256 127.0.0.1
psql -h ::1 -U postgres -d postgres TCP IPv6 host all all ::1/128 scram-sha-256 ::1
psql -h localhost -U postgres -d postgres TCP (tries ::1 first) host all all ::1/128 scram-sha-256 localhost
psql -h 192.168.2.101 -U teja -d orcl TCP from another host host all all 192.168.2.0/24 scram-sha-256 192.168.2.101

2. Unix Socket (no -h)

When you do not give -h, psql uses the Unix socket file on the same server. This works only on the database server itself, and no IP address is involved. It matches this line in pg_hba.conf: local all all scram-sha-256

[postgres@pglab01 ~]$ psql -U postgres -d postgres
Password for user postgres:
psql (17.12)
Type "help" for help.

postgres=# \conninfo
You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".
postgres=#

3. TCP Using IPv4 Loopback (-h 127.0.0.1)

This is a TCP connection to the IPv4 loopback address. It matches this line: host all all 127.0.0.1/32 scram-sha-256

[postgres@pglab01 ~]$ psql -h 127.0.0.1 -p 5432 -U postgres -d postgres
Password for user postgres:
psql (17.12)
Type "help" for help.

postgres=# \conninfo
You are connected to database "postgres" as user "postgres" on host "127.0.0.1" at port "5432".
postgres=#

4. TCP Using IPv6 Loopback (-h ::1)

This is a TCP connection to the IPv6 loopback address. It matches this line: host all all ::1/128 scram-sha-256

[postgres@pglab01 ~]$ psql -h ::1 -p 5432 -U postgres -d postgres
Password for user postgres:
psql (17.12)
Type "help" for help.

postgres=# \conninfo
You are connected to database "postgres" as user "postgres" on host "::1" at port "5432".
postgres=#

5. TCP Using localhost (-h localhost)

localhost is a TCP connection, not the Unix socket. It usually resolves to both ::1 and 127.0.0.1, and psql tries ::1 first. So you need this line in pg_hba.conf: host all all ::1/128 scram-sha-256

[postgres@pglab01 ~]$ psql -h localhost -p 5432 -U postgres -d postgres
Password for user postgres:
psql (17.12)
Type "help" for help.

postgres=# \conninfo
You are connected to database "postgres" as user "postgres" on host "localhost" (address "::1") at port "5432".
postgres=#

postgres=# select inet_client_addr();
 inet_client_addr
------------------
 ::1
(1 row)

postgres=#

If the ::1/128 line is commented out, the connection fails, and psql does not fall back to 127.0.0.1.

[postgres@pglab01 ~]$ cat /pgdata/pgsql-17/data/pg_hba.conf
# TYPE     DATABASE   USER      ADDRESS         METHOD           OPTIONS
local      all        all                       scram-sha-256
host       all        all       127.0.0.1/32    scram-sha-256
#host       all        all       ::1/128         scram-sha-256
#host       all        all       192.168.2.0/24  scram-sha-256

[postgres@pglab01 ~]$ pg_ctl reload
server signaled
[postgres@pglab01 ~]$
[postgres@pglab01 ~]$ psql -h localhost -p 5432 -U postgres -d postgres -c "select current_user;"
psql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL:  no pg_hba.conf entry for host "::1", user "postgres", database "postgres", no encryption
[postgres@pglab01 ~]$

Fix: uncomment the ::1/128 line and run pg_ctl reload, or use -h 127.0.0.1.

6. Connect to a Remote Host (-h IP address)

Run this from another server (here pglab02). It matches this line on the database server: host all all 192.168.2.0/24 scram-sha-256. The server must also listen on that address (listen_addresses in postgresql.conf).

On pglab01 :

[postgres@pglab01 ~]$ cat /pgdata/pgsql-17/data/pg_hba.conf
# TYPE     DATABASE   USER      ADDRESS         METHOD           OPTIONS
local      all        all                       scram-sha-256
host       all        all       127.0.0.1/32    scram-sha-256
host       all        all       ::1/128         scram-sha-256
host       all        all       192.168.2.0/24  scram-sha-256

[postgres@pglab01 ~]$ pg_ctl reload
server signaled
[postgres@pglab01 ~]$

On pglab02 :

[postgres@pglab02 ~]$ hostname -i
192.168.2.102
[postgres@pglab02 ~]$
[postgres@pglab02 ~]$ psql -h 192.168.2.101 -p 5432 -U teja -d orcl
Password for user teja:
psql (17.12)
Type "help" for help.

orcl=> \conninfo
You are connected to database "orcl" as user "teja" on host "192.168.2.101" at port "5432".
orcl=>

You can also use a host name instead of the IP address, if it resolves (DNS or /etc/hosts). Then the .pgpass host field must be that same name:

[postgres@pglab02 ~]$ psql -h pglab01 -p 5432 -U teja -d orcl

7. Connect Using a Socket Directory (-h /path)

If the value after -h starts with a /, psql treats it as the directory of the Unix socket. This also matches this line: local all all scram-sha-256

[postgres@pglab01 ~]$ psql -h /var/run/postgresql -p 5432 -U postgres -d postgres
Password for user postgres:
psql (17.12)
Type "help" for help.

postgres=#
postgres=# \conninfo
You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".
postgres=#

This is useful when the server has more than one socket directory, or when a different PostgreSQL instance uses a different port.

8. Connect Using a Connection String or URI

Instead of separate options, you can give everything in one quoted string. Both forms below connect to the same database.

# key=value connection string
[postgres@pglab01 ~]$ psql "host=localhost port=5432 dbname=postgres user=postgres"

# URI format (postgresql://user@host:port/database)
[postgres@pglab01 ~]$ psql "postgresql://postgres@localhost:5432/postgres"

# Remote host, with SSL required
[postgres@pglab02 ~]$ psql "postgresql://teja@192.168.2.101:5432/orcl?sslmode=require"

You can also set the connection details as environment variables, and then run psql with no options:

[postgres@pglab02 ~]$ export PGHOST=192.168.2.101 PGPORT=5432 PGDATABASE=orcl PGUSER=teja
[postgres@pglab02 ~]$ psql
Password for user teja:
psql (17.12)
Type "help" for help.

orcl=> \conninfo
You are connected to database "orcl" as user "teja" on host "192.168.2.101" at port "5432".
orcl=>

Do not put the password in the connection string or URI. It is visible in the process list and in your shell history. Use .pgpass instead.

9. Password Options (-w, -W, .pgpass, PGPASSWORD)

# -W : always ask for the password (even if .pgpass has it)
[postgres@pglab01 ~]$ psql -h localhost -U postgres -d postgres -W
Password:

# -w : never ask. Works only if .pgpass or PGPASSWORD supplies the password, otherwise it fails
[postgres@pglab01 ~]$ psql -h localhost -U postgres -d postgres -w -c "select current_user;"
 current_user
--------------
 postgres
(1 row)

# PGPASSWORD for a single command (checked before .pgpass)
[postgres@pglab01 ~]$ PGPASSWORD='oracle' psql -h localhost -U postgres -d postgres -c "select now();"
  • .pgpass must have permission 0600, or it is ignored.
  • .pgpass and PGPASSWORD are used only when the server asks for a password (scram-sha-256 or md5). They are not used with trust or peer.

10. Run a Command or a File and Exit (-c, -f)

# Run one SQL command and exit
[postgres@pglab01 ~]$ psql -h localhost -U postgres -d postgres -c "select version();"

# Run a SQL file and exit
[postgres@pglab01 ~]$ psql -h localhost -U postgres -d postgres -f /tmp/script.sql

# Run a SQL file, stop at the first error
[postgres@pglab01 ~]$ psql -h localhost -U postgres -d postgres -v ON_ERROR_STOP=1 -f /tmp/script.sql

# Output only the value, no headers or alignment
[postgres@pglab01 ~]$ psql -h localhost -U postgres -d postgres -Atc "select current_user;"
postgres

11. Verify How You Are Connected

After you connect, check which type of connection you got and which pg_hba.conf line was used:

postgres=# \conninfo                   <---- shows "via socket" or "on host ... at port ..."
postgres=# select inet_client_addr();   <---- empty = socket (local), 127.0.0.1 = IPv4, ::1 = IPv6, 192.168.2.x = remote
postgres=# select inet_server_addr(), inet_server_port();   <---- the server address and port you reached
Caution: Your use of any information or materials on this website is entirely at your own risk. It is provided for educational purposes only. It has been tested internally, however, we do not guarantee that it will work for you. Ensure that you run it in your test environment before using.
Thank you
Rajasekhar Amudala