— opens a persistent connection to a remote database
— opens a persistent connection to a remote database, insecurely
— closes a persistent connection to a remote database
— executes a query in a remote database
— executes a command in a remote database
— opens a cursor in a remote database
— returns rows from an open cursor in a remote database
— closes a cursor in a remote database
— returns the names of all open named dblink connections
— gets last error message on the named connection
— sends an async query to a remote database
— checks if connection is busy with an async query
— retrieve async notifications on a connection
— gets an async query result
— cancels any active query on the named connection
— returns the positions and field names of a relation's primary key fields
— builds an INSERT statement using a local tuple, replacing the primary key field values with alternative supplied values
— builds a DELETE statement using supplied values for primary key field values
— builds an UPDATE statement using a local tuple, replacing the primary key field values with alternative supplied values
dblink
is a module that supports connections to otherPostgreSQLdatabases from within a database session.
See alsopostgres_fdw, which provides roughly the same functionality using a more modern and standards-compliant infrastructure.
dblink — executes a query in a remote database
dblink
executes a query (usually aSELECT
, but it can be any SQL statement that returns rows) in a remote database.
When twotext
arguments are given, the first one is first looked up as a persistent connection's name; if found, the command is executed on that connection. If not found, the first argument is treated as a connection info string as fordblink_connect
, and the indicated connection is made just for the duration of this command.
connname
Name of the connection to use; omit this parameter to use the unnamed connection.
connstr
A connection info string, as previously described fordblink_connect
.
sql
The SQL query that you wish to execute in the remote database, for exampleselect * from foo
.
fail_on_error
If true (the default when omitted) then an error thrown on the remote side of the connection causes an error to also be thrown locally. If false, the remote error is locally reported as a NOTICE, and the function returns no rows.
The function returns the row(s) produced by the query. Sincedblink
can be used with any query, it is declared to returnrecord
, rather than specifying any particular set of columns. This means that you must specify the expected set of columns in the calling query — otherwisePostgreSQLwould not know what to expect. Here is an example:
The“alias”part of theFROM
clause must specify the column names and types that the function will return. (Specifying column names in an alias is actually standard SQL syntax, but specifying column types is aPostgreSQLextension.) This allows the system to understand what*
should expand to, and whatproname
in theWHERE
clause refers to, in advance of trying to execute the function. At run time, an error will be thrown if the actual query result from the remote database does not have the same number of columns shown in theFROM
clause. The column names need not match, however, anddblink
does not insist on exact type matches either. It will succeed so long as the returned data strings are valid input for the column type declared in theFROM
clause.
A convenient way to usedblink
with predetermined queries is to create a view. This allows the column type information to be buried in the view, instead of having to spell it out in every query. For example,