Logo Wael's Digital Garden

PostgreSQL - psql using URI

Problem#

I have a PostgreSQL URI for one of my applications, a URI such as postgres://username:password@hostname:port/database-name and I need to connect to the database from the command-line

Solution#

I used to break the URI into its parts and pass them to psql in the usual way (PGPASSWORD for the password, -h <host> for the host, -U <user> for the user etc..

However, it turned out that psql works just fine with the URI and so the following command works just fine:

$ psql "postgres://username:password@hostname:port/database-name"

Related#