DBLINK(3)
hey bestie π«Ά you're reading DBLINK(3) β the PostgreSQL 13.23 Documentation page for dblink, and omg we're gonna make it make sense β¨
π NAME π
dblink β executes a query in a remote database. that's literally the whole name section, and it ate π
πΈ SYNOPSIS πΈ
three iconic flavors, bestie:
dblink(text connname, text sql [, bool fail_on_error]) returns setof record
dblink(text connstr, text sql [, bool fail_on_error]) returns setof record
dblink(text sql [, bool fail_on_error]) returns setof record
π¦ DESCRIPTION π¦
dblink executes a query (usually a SELECT, but it can be any SQL statement that returns rows) in a remote database. lowkey powerful π«Ά
when two text 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 for dblink_connect, and the indicated connection is made just for the duration of this command. no cap.
π ARGUMENTS π
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β iftrue(the default when omitted) then an error thrown on the remote side of the connection causes an error to also be thrown locally. iffalse, the remote error is locally reported as aNOTICE, and the function returns no rows. π
π₯ RETURN VALUE π₯
the function returns the row(s) produced by the query. since dblink can be used with any query, it is declared to return record, rather than specifying any particular set of columns. this means that you must specify the expected set of columns in the calling query β otherwise PostgreSQL would not know what to expect. here's an example, bestie:
SELECT *
FROM dblink('dbname=mydb options=-csearch_path=',
'select proname, prosrc from pg_proc')
AS t1(proname name, prosrc text)
WHERE proname LIKE 'bytea%';
the alias part of the FROM 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 a PostgreSQL extension.) this allows the system to understand what * should expand to, and what proname in the WHERE 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 the FROM clause. the column names need not match, however, and dblink 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 the FROM clause. periodt π
π NOTES π
a convenient way to use dblink 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:
CREATE VIEW myremote_pg_proc AS
SELECT *
FROM dblink('dbname=postgres options=-csearch_path=',
'select proname, prosrc from pg_proc')
AS t1(proname name, prosrc text);
SELECT * FROM myremote_pg_proc WHERE proname LIKE 'bytea%';
β EXAMPLES β
okay demo time, bestie π these are the examples, and yes we're keeping every row:
SELECT * FROM dblink('dbname=postgres options=-csearch_path=',
'select proname, prosrc from pg_proc')
AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
proname | prosrc
------------+------------
byteacat | byteacat
byteaeq | byteaeq
bytealt | bytealt
byteale | byteale
byteagt | byteagt
byteage | byteage
byteane | byteane
byteacmp | byteacmp
bytealike | bytealike
byteanlike | byteanlike
byteain | byteain
byteaout | byteaout
(12 rows)
SELECT dblink_connect('dbname=postgres options=-csearch_path=');
dblink_connect
----------------
OK
(1 row)
SELECT * FROM dblink('select proname, prosrc from pg_proc')
AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
proname | prosrc
------------+------------
byteacat | byteacat
byteaeq | byteaeq
bytealt | bytealt
byteale | byteale
byteagt | byteagt
byteage | byteage
byteane | byteane
byteacmp | byteacmp
bytealike | bytealike
byteanlike | byteanlike
byteain | byteain
byteaout | byteaout
(12 rows)
SELECT dblink_connect('myconn', 'dbname=regression options=-csearch_path=');
dblink_connect
----------------
OK
(1 row)
SELECT * FROM dblink('myconn', 'select proname, prosrc from pg_proc')
AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
proname | prosrc
------------+------------
bytearecv | bytearecv
byteasend | byteasend
byteale | byteale
byteagt | byteagt
byteage | byteage
byteane | byteane
byteacmp | byteacmp
bytealike | bytealike
byteanlike | byteanlike
byteacat | byteacat
byteaeq | byteaeq
bytealt | bytealt
byteain | byteain
byteaout | byteaout
(14 rows)
that's the whole page, bestie π PostgreSQL 13.23 Β· 2025 Β· DBLINK(3) β mwah π bye π«Ά
π¦ where this page comes from
- opensuse-leap 16.0: postgresql13-docs 13.23-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms