How can I connect to a PostgreSQL database using the command line?

2023-06-20

Docshowtosnippet

Use psql.

On localhost you can login with:

$ psql -U <username> -W <password> <database name>

In default settings, your username is postgres and you can leave the password blank

$ psql -U postgres <database name>

#Making a remote connection

$ psql -U <username> -W <password> -p <port> -h <host> <database name>

If you have a connection string you can also simply pass in the connection string to psql

$ psql postgresql://user@hostname:5433/database_name?sslmode=require

The above command will prompt for a password (or pass in -w for no password)