user5047085
user5047085

Reputation:

Test postgresql connection string using psql at bash command line?

If I have a postgresql connection string:

export my_conn='postgresql://vadmin:[email protected]/prod'

how can I test this connection? I tried:

pqsl "$my_conn"

and it just hangs

Upvotes: 16

Views: 24541

Answers (2)

Laurenz Albe
Laurenz Albe

Reputation: 246238

The best tool for the purpose is pg_isready.

Simply run

pg_isready -d "$my_conn"

and check the return code.

Upvotes: 12

Cvetan Mihaylov
Cvetan Mihaylov

Reputation: 950

Run this instead:

psql "$my_conn" -c "SELECT 1"

This would try to execute a simple query that always should return a one-row result and then exit. Also you could check the exit code of the operation by calling:

echo $?

Anything different than 0 would mean some error.

Upvotes: 14

Related Questions