PostgreSQL 18 cheatsheet

PostgreSQL 18: Connecting

6 common PostgreSQL 18 tasks.

Check the client version

Print the version of the psql client.

psql --version

Connect to a database

Open psql against a specific host, port, user and database.

psql -h localhost -p 5432 -U postgres -d mydb

Connect with a connection URI

Open psql using a single connection string.

psql "postgresql://postgres@localhost:5432/mydb"

Check the server version

Run inside psql to see which server version you are connected to.

SELECT version();

Quit psql

Leave the psql prompt and return to the shell.

\q

Run a SQL file

Execute the statements in a file and stop at the first error.

psql -U postgres -d mydb -v ON_ERROR_STOP=1 -f script.sql

Was this page helpful?