When using this library's way of connecting to a server, i.e. with PQconnectStart, instead of PQconnectdb, I get a CONNECTION_BAD result. When I switched to PQconnectdb, it worked fine. Maybe because of this line:
Use PQsocket(conn) to obtain the descriptor of the socket underlying the database connection. (Caution: do not assume that the socket remains the same across PQconnectPoll calls.)
from https://www.postgresql.org/docs/current/libpq-connect.html