DBLINK_EXEC(3)

okay bestie, welcome to dblink_exec(3), straight out of the PostgreSQL 18.6 Documentation ✨ this is the page where we yeet SQL commands into a remote database and don't even care about getting rows back. no rows, no problem, we're just here to make stuff happen 🫢


✨ NAME ✨

dblink_exec β€” executes a command in a remote database πŸ’«


✨ SYNOPSIS ✨

dblink_exec(text connname, text sql [, bool fail_on_error]) returns text
dblink_exec(text connstr, text sql [, bool fail_on_error]) returns text
dblink_exec(text sql [, bool fail_on_error]) returns text

✨ DESCRIPTION ✨

so like. dblink_exec executes a command β€” and by "command" we mean any SQL statement that doesn't return rows β€” in a remote database. that's it, that's the vibe. it's giving "do the thing, don't text me back" πŸ“΅

when you pass it two text arguments, here's the lil algorithm, no cap:

  1. the first argument gets looked up as a persistent connection's name first πŸ‘€
  2. if it's found, boom β€” the command runs on that connection 🎯
  3. if it's not found, the first argument gets treated as a connection info string, exactly like for dblink_connect, and the connection it describes gets made just for the duration of this command β€” a little pop-up connection, here for a good time not a long time πŸ¦‹

✨ ARGUMENTS ✨

  • connname β€” the name of the connection you wanna use πŸ’œ omit this parameter entirely and you get the unnamed connection instead (she's mysterious, she's unnamed, she's available).
  • connstr β€” a connection info string, same as the one previously described for dblink_connect πŸ”—
  • sql β€” the SQL command you wish to execute in the remote database πŸš€ for example insert into foo values(0, 'a', '{"a0","b0","c0"}').
  • fail_on_error β€” if it's true (which is the default when you omit it, obviously, she's the default for a reason πŸ’…) then an error thrown on the remote side of the connection causes an error to also be thrown locally. if it's false, the remote error gets locally reported as a NOTICE instead, and the function's return value is set to ERROR. so like... quiet mode. graceful degradation. she's unbothered 😌

✨ RETURN VALUE ✨

returns status β€” either the command's status string or ERROR. that's the whole tea 🍡


✨ EXAMPLES ✨

okay this is the part where we actually do it, watch πŸ‘‘

SELECT dblink_connect('dbname=dblink_test_standby');
 dblink_connect
----------------
 OK
(1 row)

SELECT dblink_exec('insert into foo values(21, ''z'', ''{"a0","b0","c0"}'');');
   dblink_exec
-----------------
 INSERT 943366 1
(1 row)

SELECT dblink_connect('myconn', 'dbname=regression');
 dblink_connect
----------------
 OK
(1 row)

SELECT dblink_exec('myconn', 'insert into foo values(21, ''z'', ''{"a0","b0","c0"}'');');
   dblink_exec
------------------
 INSERT 6432584 1
(1 row)

SELECT dblink_exec('myconn', 'insert into pg_class values (''foo'')',false);
NOTICE:  sql error
DETAIL:  ERROR:  null value in column "relnamespace" violates not-null constraint

 dblink_exec
-------------
 ERROR
(1 row)

see how the last one used false and just slid into a NOTICE instead of fully exploding? that's the move. she said "i'll tell you about it but i won't ruin your whole afternoon" 🌸

πŸ’€ heads up bestie: that last example is a real error happening on purpose β€” null value in column "relnamespace" violates not-null constraint. the false is what keeps it from being thrown locally. don't @ me, that's literally what the docs show.

mwah πŸ’‹ that's the whole page! this was dblink_exec(3) from the PostgreSQL 18.6 Documentation, dated 2026. go execute something in a remote database, you absolute icon πŸ‘‘βœ¨ bye bestie! πŸŽ€πŸ¦‹πŸ’–