1DBLINK(3)               PostgreSQL 9.2.24 Documentation              DBLINK(3)
2
3
4

NAME

6       dblink - executes a query in a remote database
7

SYNOPSIS

9       dblink(text connname, text sql [, bool fail_on_error]) returns setof record
10       dblink(text connstr, text sql [, bool fail_on_error]) returns setof record
11       dblink(text sql [, bool fail_on_error]) returns setof record
12

DESCRIPTION

14       dblink executes a query (usually a SELECT, but it can be any SQL
15       statement that returns rows) in a remote database.
16
17       When two text arguments are given, the first one is first looked up as
18       a persistent connection's name; if found, the command is executed on
19       that connection. If not found, the first argument is treated as a
20       connection info string as for dblink_connect, and the indicated
21       connection is made just for the duration of this command.
22

ARGUMENTS

24       connname
25           Name of the connection to use; omit this parameter to use the
26           unnamed connection.
27
28       connstr
29           A connection info string, as previously described for
30           dblink_connect.
31
32       sql
33           The SQL query that you wish to execute in the remote database, for
34           example select * from foo.
35
36       fail_on_error
37           If true (the default when omitted) then an error thrown on the
38           remote side of the connection causes an error to also be thrown
39           locally. If false, the remote error is locally reported as a
40           NOTICE, and the function returns no rows.
41

RETURN VALUE

43       The function returns the row(s) produced by the query. Since dblink can
44       be used with any query, it is declared to return record, rather than
45       specifying any particular set of columns. This means that you must
46       specify the expected set of columns in the calling query — otherwise
47       PostgreSQL would not know what to expect. Here is an example:
48
49           SELECT *
50               FROM dblink('dbname=mydb', 'select proname, prosrc from pg_proc')
51                 AS t1(proname name, prosrc text)
52               WHERE proname LIKE 'bytea%';
53
54       The “alias” part of the FROM clause must specify the column names and
55       types that the function will return. (Specifying column names in an
56       alias is actually standard SQL syntax, but specifying column types is a
57       PostgreSQL extension.) This allows the system to understand what *
58       should expand to, and what proname in the WHERE clause refers to, in
59       advance of trying to execute the function. At run time, an error will
60       be thrown if the actual query result from the remote database does not
61       have the same number of columns shown in the FROM clause. The column
62       names need not match, however, and dblink does not insist on exact type
63       matches either. It will succeed so long as the returned data strings
64       are valid input for the column type declared in the FROM clause.
65

NOTES

67       A convenient way to use dblink with predetermined queries is to create
68       a view. This allows the column type information to be buried in the
69       view, instead of having to spell it out in every query. For example,
70
71           CREATE VIEW myremote_pg_proc AS
72             SELECT *
73               FROM dblink('dbname=postgres', 'select proname, prosrc from pg_proc')
74               AS t1(proname name, prosrc text);
75
76           SELECT * FROM myremote_pg_proc WHERE proname LIKE 'bytea%';
77

EXAMPLES

79           SELECT * FROM dblink('dbname=postgres', 'select proname, prosrc from pg_proc')
80             AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
81             proname   |   prosrc
82           ------------+------------
83            byteacat   | byteacat
84            byteaeq    | byteaeq
85            bytealt    | bytealt
86            byteale    | byteale
87            byteagt    | byteagt
88            byteage    | byteage
89            byteane    | byteane
90            byteacmp   | byteacmp
91            bytealike  | bytealike
92            byteanlike | byteanlike
93            byteain    | byteain
94            byteaout   | byteaout
95           (12 rows)
96
97           SELECT dblink_connect('dbname=postgres');
98            dblink_connect
99           ----------------
100            OK
101           (1 row)
102
103           SELECT * FROM dblink('select proname, prosrc from pg_proc')
104             AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
105             proname   |   prosrc
106           ------------+------------
107            byteacat   | byteacat
108            byteaeq    | byteaeq
109            bytealt    | bytealt
110            byteale    | byteale
111            byteagt    | byteagt
112            byteage    | byteage
113            byteane    | byteane
114            byteacmp   | byteacmp
115            bytealike  | bytealike
116            byteanlike | byteanlike
117            byteain    | byteain
118            byteaout   | byteaout
119           (12 rows)
120
121           SELECT dblink_connect('myconn', 'dbname=regression');
122            dblink_connect
123           ----------------
124            OK
125           (1 row)
126
127           SELECT * FROM dblink('myconn', 'select proname, prosrc from pg_proc')
128             AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
129             proname   |   prosrc
130           ------------+------------
131            bytearecv  | bytearecv
132            byteasend  | byteasend
133            byteale    | byteale
134            byteagt    | byteagt
135            byteage    | byteage
136            byteane    | byteane
137            byteacmp   | byteacmp
138            bytealike  | bytealike
139            byteanlike | byteanlike
140            byteacat   | byteacat
141            byteaeq    | byteaeq
142            bytealt    | bytealt
143            byteain    | byteain
144            byteaout   | byteaout
145           (14 rows)
146
147
148
149PostgreSQL 9.2.24                 2017-11-06                         DBLINK(3)
Impressum