How Do You Test the Database Credentials (username and password) of a PostgreSQL Database Using Bash?

Problem scenario
You are not sure if the username and password to a PostgreSQL database work.  What do you do?

Solution
Run this one-line command:

psql -d "postgresql://jdoe:coolpassword@localhost/foobardb" -c "select now()"

# Replace "jdoe" with the SQL database username
# Replace "coolpassword" with the user's password
# Replace "foobardb" with the name of the database you are authenticating too
# If you have the PostgreSQL client installed (so psql works) and the database is remote, use the FQDN instead of the "localhost"

This solution was based on this posting.

Leave a comment

Your email address will not be published. Required fields are marked *