pg_close

(PHP 4, PHP 5, PHP 7, PHP 8)

pg_closeCloses a PostgreSQL connection

Description

pg_close(?PgSql\Connection $connection = null): true

pg_close() closes the non-persistent connection to a PostgreSQL database associated with the given connection instance.

Note:

Using pg_close() is not usually necessary, as non-persistent open connections are automatically closed at the end of the script.

If there is open PgSql\Lob instance on the connection, do not close the connection before closing all PgSql\Lob instances.

Parameters

connection

An PgSql\Connection instance. When connection is null, the default connection is used. The default connection is the last connection made by pg_connect() or pg_pconnect().

Warning

As of PHP 8.1.0, using the default connection is deprecated.

Return Values

Always returns true.

Changelog

Version Description
8.2.0 The return type is true now; previously, it was bool.
8.1.0 The connection parameter expects an PgSql\Connection instance now; previously, a resource was expected.
8.0.0 connection is now nullable.

Examples

Example #1 pg_close() example

<?php
$dbconn
= pg_connect("host=localhost port=5432 dbname=mary")
or die(
"Could not connect");
echo
"Connected successfully";
pg_close($dbconn);
?>

The above example will output:

Connected successfully

See Also

add a note

User Contributed Notes 2 notes

up
6
amays
19 years ago
pg_close(...) will not technically close a persistent connection but instead returns it back to the connection pool thus giving you the desired effect of having the connection closed within your script.

http://www.sitepoint.com/article/accessing-postgresql-php/3

best wishes to all.
up
2
mark at redbrick dot dcu dot ie
21 years ago
This function closes the current database connection specified by a handle returned from a pg_connect() call.

<?php
$pgsql_conn
= pg_connect("dbname=mark host=localhost");

if (
$pgsql_conn) {
print
"Successfully connected to: " . pg_host($pgsql_conn) . "<br/>\n";
} else {
print
pg_last_error($pgsql_conn);
exit;
}

// Do database stuff here.

if(!pg_close($pgsql_conn)) {
print
"Failed to close connection to " . pg_host($pgsql_conn) . ": " .
pg_last_error($pgsql_conn) . "<br/>\n";
} else {
print
"Successfully disconnected from database";
}
?>

Of course you normally wouldn't print a message.

Regards, --mark
To Top