For a healthcheck or a startup script I only need a yes or no, with no login. PostgreSQL ships a tool for that.
pg_isready -h 127.0.0.1 -p 5432
echo $?
pg_isready connects, does not log in, and reports through its exit code: 0 means the server accepts connections, 1 means it is up but rejecting them (for example while starting), 2 means no response and 3 means the check could not be attempted.
That makes it handy in a Docker healthcheck or a loop that waits before an app starts. It tells you the server is reachable, not that your user, password or database name are correct.
Further reading: PostgreSQL documentation: pg_isready