How do you add connection options to PSQL?

Viewed 754

Some Postgres connection options are usually specified in the connection string. For example, sslmode=require can be set with the following connection string

postgresql://postgres:postgres@localhost:5432/postgres?sslmode=require

But psql --help doesn't provide any information on how to set something like that.

1 Answers

You can pass in a conninfo string or URI. The two examples below are equivalent.

psql postgresql://postgres:postgres@localhost:5432/postgres?sslmode=require
psql "sslmode=require" -U postgres -h localhost -p 5432 -d postgres

Multiple options are space delineated.

psql "dbname=postgres sslmode=require" -U postgres -h localhost -p 5432

See The Connection URI Parameter Key Words documentation for other options.

You can also inspect the current connection info using the \conninfo meta command

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

The online documentation of psql goes into more detail under the Connecting to a Database section.

Related