18

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

1
  • 1
    I'm going to assume that the typo above (pqsl vs psql) is just a typo in creating the question. If the above is hanging, I'd expect that your issue is some kind of connection issue between your server and the database you are connecting to. Maybe a firewall or routing issue, but also maybe a shell expansion issue. You might want to try to reformulate the connection directly from the command line using psql -U vadmin -h platform-prod.clfk51eo.us-west-2.rds.amazonaws.com -d prod to see if you are able to connect that way. Commented Jun 13, 2019 at 23:07

2 Answers 2

18

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.

Sign up to request clarification or add additional context in comments.

Comments

13

The best tool for the purpose is pg_isready.

Simply run

pg_isready -d "$my_conn"

and check the return code.

3 Comments

thanks, is there a way to open up a shell, something like psql>
not entirely, if you can open a shell, you can connect
I thought you wanted to test the connection. Why would you want an interactive shell for that?