Updated on 2025-05-29 GMT+08:00

Closing a Connection

libpq usually uses the function PQfinish to close the connection to the database. If your application has established multiple connections, make sure that each connection is closed correctly.

The following is an example (for details about the complete example, see Establishing a Database Connection, Executing SQL Statements, and Returning Results).

/* Close the portal. We do not need to check for errors. */
res = PQexec(conn, "CLOSE myportal");
PQclear(res);

/* End the transaction. */
res = PQexec(conn, "END");
PQclear(res);

/* Close the database connection and clean up the database. */
PQfinish(conn);