dblink_connect <refpurpose>opens a persistent connection to a remote database</refpurpose> — リモートデータベースへの永続的な接続を開く
dblink_connect(text connstr) returns text dblink_connect(text connname, text connstr) returns text
<function>dblink_connect()</function> establishes a connection to a remote
<productname>PostgreSQL</productname> database. The server and database to
be contacted are identified through a standard <application>libpq</application>
connection string. Optionally, a name can be assigned to the
connection. Multiple named connections can be open at once, but
only one unnamed connection is permitted at a time. The connection
will persist until closed or until the database session is ended.
dblink_connect()はリモートのPostgreSQLデータベースへの接続を確立します。
接続先のサーバとデータベースは標準のlibpq接続文字列を通して識別されます。
省略可能ですが、名前を接続に割り当てることも可能です。
複数の名前付きの接続を一度に開くことができますが、無名の接続は同時に1つしか許されません。
接続は、閉ざされるまで、または、データベースセッションが終わるまで永続します。
The connection string may also be the name of an existing foreign
server. It is recommended to use the foreign-data wrapper
<literal>dblink_fdw</literal> when defining the foreign
server. See the example below, as well as
<xref linkend="sql-createserver"/> and
<xref linkend="sql-createusermapping"/>.
接続文字列は同時に既存の外部サーバ名であっても構いません。
外部サーバを定義する場合、外部データラッパーdblink_fdwを使用することを推奨します。
後述の例とCREATE SERVER、CREATE USER MAPPINGを参照してください。
connnameThe name to use for this connection; if omitted, an unnamed connection is opened, replacing any existing unnamed connection. 接続に使用する名前です。省略した場合、既存の無名の接続を閉ざし、無名の接続を開きます。
connstr
例えばhostaddr=127.0.0.1 port=5432 dbname=mydb user=postgres password=mypasswdといったlibpq形式の接続情報文字列です。
詳細は32.1.1を参照してください。
もしくは外部サーバ名です。
Returns status, which is always <literal>OK</literal> (since any error
causes the function to throw an error instead of returning).
状態を返します。
これは常にOKです(何らかのエラーが起きるとこの関数は戻らずエラーとなるためです)。
If untrusted users have access to a database that has not adopted a
<link linkend="ddl-schemas-patterns">secure schema usage pattern</link>,
begin each session by removing publicly-writable schemas from
<varname>search_path</varname>. One could, for example,
add <literal>options=-csearch_path=</literal> to
<parameter>connstr</parameter>. This consideration is not specific
to <filename>dblink</filename>; it applies to every interface for
executing arbitrary SQL commands.
信頼できないユーザが、安全なスキーマ使用パターンを適用していないデータベースへアクセスする際には、セッション開始時にsearch_pathから、第三者が書き込みができるスキーマを削除してください。
これはたとえばconnstrに値-csearch_path=を設定することで可能となります。
このような配慮は、dblinkに限ったことではありません。
任意のSQLコマンドを実行するすべてのインタフェースに当てはまります。
The foreign-data wrapper <filename>dblink_fdw</filename> has an additional
Boolean option <literal>use_scram_passthrough</literal> that controls
whether <filename>dblink</filename> will use the SCRAM pass-through
authentication to connect to the remote database. With SCRAM pass-through
authentication, <filename>dblink</filename> uses SCRAM-hashed secrets
instead of plain-text user passwords to connect to the remote server. This
avoids storing plain-text user passwords in PostgreSQL system catalogs.
See the documentation of the equivalent <link
linkend="postgres-fdw-option-use-scram-passthrough"><literal>use_scram_passthrough</literal></link>
option of postgres_fdw for further details and restrictions.
外部データラッパーdblink_fdwには、追加のブールオプションuse_scram_passthroughがあり、dblinkがSCRAMパススルー認証を使用してリモートデータベースに接続するかどうかを制御します。
SCRAMパススルー認証では、dblinkはプレーンテキストユーザパスワードの代わりにSCRAMハッシュ化されたシークレットを使用してリモートサーバに接続します。
これにより、プレーンテキストユーザパスワードがPostgreSQLシステムカタログに格納されるのを回避します。
詳細と制限については、postgres_fdwの相当するuse_scram_passthroughオプションの文書を参照してください。
Only superusers may use <function>dblink_connect</function> to create
connections that use neither password authentication, SCRAM pass-through,
nor GSSAPI-authentication.
If non-superusers need this capability, use
<function>dblink_connect_u</function> instead.
スーパーユーザのみがパスワード認証やSCRAMパススルー、GSSAPI認証がない接続を作成するためにdblink_connectを使用できます。
スーパーユーザ以外でこの機能が必要ならばdblink_connect_uを代わりに使用してください。
It is unwise to choose connection names that contain equal signs,
as this opens a risk of confusion with connection info strings
in other <filename>dblink</filename> functions.
他のdblink関数内で接続情報文字列が混乱する危険が発生しますので、等号記号を含む接続名を選択することは勧めません。
SELECT dblink_connect('dbname=postgres options=-csearch_path=');
dblink_connect
----------------
OK
(1 row)
SELECT dblink_connect('myconn', 'dbname=postgres options=-csearch_path=');
dblink_connect
----------------
OK
(1 row)
-- FOREIGN DATA WRAPPER functionality
-- Note: local connections that don't use SCRAM pass-through require password
-- authentication for this to work properly. Otherwise, you will receive
-- the following error from dblink_connect():
-- 外部データラッパー(FOREIGN DATA WRAPPER)の機能
-- 注意:SCRAMパススルーを使わないローカル接続でこれが正常に機能するには
-- パスワード認証が必須です。
-- さもないと、dblink_connect()から以下のエラーを受け取ります。
-- ERROR: password is required
-- DETAIL: Non-superuser cannot connect if the server does not request a password.
-- HINT: Target server's authentication method must be changed.
CREATE SERVER fdtest FOREIGN DATA WRAPPER dblink_fdw OPTIONS (hostaddr '127.0.0.1', dbname 'contrib_regression');
CREATE USER regress_dblink_user WITH PASSWORD 'secret';
CREATE USER MAPPING FOR regress_dblink_user SERVER fdtest OPTIONS (user 'regress_dblink_user', password 'secret');
GRANT USAGE ON FOREIGN SERVER fdtest TO regress_dblink_user;
GRANT SELECT ON TABLE foo TO regress_dblink_user;
\set ORIGINAL_USER :USER
\c - regress_dblink_user
SELECT dblink_connect('myconn', 'fdtest');
dblink_connect
----------------
OK
(1 row)
SELECT * FROM dblink('myconn', 'SELECT * FROM foo') AS t(a int, b text, c text[]);
a | b | c
----+---+---------------
0 | a | {a0,b0,c0}
1 | b | {a1,b1,c1}
2 | c | {a2,b2,c2}
3 | d | {a3,b3,c3}
4 | e | {a4,b4,c4}
5 | f | {a5,b5,c5}
6 | g | {a6,b6,c6}
7 | h | {a7,b7,c7}
8 | i | {a8,b8,c8}
9 | j | {a9,b9,c9}
10 | k | {a10,b10,c10}
(11 rows)
\c - :ORIGINAL_USER
REVOKE USAGE ON FOREIGN SERVER fdtest FROM regress_dblink_user;
REVOKE SELECT ON TABLE foo FROM regress_dblink_user;
DROP USER MAPPING FOR regress_dblink_user SERVER fdtest;
DROP USER regress_dblink_user;
DROP SERVER fdtest;