DBLINK(3)
π
DBLINK(3) β the dblink page, bestie edition β¨
okay so this is the PostgreSQL dblink manual page (we're on PostgreSQL 15.19, keep that in your notes π) and honestly? it's gonna change your whole remote-query situation. stay with me π«Ά
β¨ NAME β¨
dblink β literally executes a query in a remote database. that's the whole vibe. π«
πΈ SYNOPSIS πΈ
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
three flavors, pick your fighter π the square brackets mean that last argument is optional, iykyk.
π¦ DESCRIPTION π¦
so dblink executes a query in a remote database. usually that's a SELECT, but fr fr it can be any SQL statement that returns rows β no gatekeeping here π
now here's the cute part: when you hand it two text arguments, the first one gets looked up first as a persistent connection's name. if it's found, the command runs on that connection, easy. if it's not found, that first argument gets treated as a connection info string just like the one for dblink_connect, and the indicated connection gets made just for the duration of this command. one-night-stand connection, we love to see it π
π ARGUMENTS π
connnameβ the name of the connection to use. omit this parameter and you'll be using the unnamed connection instead. main character energy, no name needed β¨connstrβ a connection info string, as previously described fordblink_connect. it's giving "go read that page too" but respectfully π«Άsqlβ the SQL query you wish to execute in the remote database, for exampleselect * from foo. go off, queen π₯fail_on_errorβ iftrue(which is the default when you omit it), then an error thrown on the remote side of the connection causes an error to also be thrown locally. loud and proud. iffalse, the remote error gets locally reported as aNOTICE, and the function returns no rows. so like, the drama gets downgraded to a whisper π
β RETURN VALUE β
the function returns the row(s) produced by the query. and because dblink can be used with any query, it's declared to return record rather than committing to any particular set of columns. commitment issues, honestly π
which means: you must specify the expected set of columns in the calling query β otherwise PostgreSQL would not know what to expect. no cap, it just wouldn't. here's an example:
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. (fun fact: specifying column names in an alias is actually standard SQL syntax, but specifying column types is a PostgreSQL extension β we're built different π
) this lets the system understand what * should expand to, and what proname in the WHERE clause refers to, before it even tries to execute the function. foresight, iconic.
at run time, an error will be thrown if the actual query result from the remote database doesn't have the same number of columns shown in the FROM clause. the column names need not match, though, and dblink doesn't 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. flexible but with boundaries, we stan π
π NOTES π
a convenient way to use dblink with predetermined queries is to create a view. this lets the column type information be buried in the view instead of having to spell it out in every single query. lazy girlie hack, but make it architecture β¨ 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 bestie, buckle up, we got four of them π’
one β inline connection string, no named connection:
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)
two β connect to the unnamed connection first:
SELECT dblink_connect('dbname=postgres options=-csearch_path=');
dblink_connect
----------------
OK
(1 row)
three β now query through that unnamed connection:
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)
four β now with a named connection called myconn:
SELECT dblink_connect('myconn', 'dbname=regression options=-csearch_path=');
dblink_connect
----------------
OK
(1 row)
and then query it by name π
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)
note that last one gave you 14 rows, not 12 β the connection points at regression this time, so the results are different. the way the row counts change based on which database you're hitting... obsessed πβ¨
anyway that's the whole page, you're welcome, go execute some remote queries responsibly π§π
source + version + date, for the record: PostgreSQL 15.19, dated 2026. mwah, bye bestie ππ
π¦ where this page comes from
- opensuse-leap 16.0: postgresql15-docs 15.19-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