如何将远程 Postgresql 数据库(链接服务器)添加到 Postgresql 数据库?
How to add a remote Postgresql db(linked server) to a Postgresql db?
我家里有一个postgresql数据库,一个在云端。我想将我的家庭数据库添加到云数据库中,以便我可以轻松地在数据库之间进行查询。如何才能做到这一点?不使用 dblink
http://www.postgresonline.com/journal/archives/44-Using-DbLink-to-access-other-PostgreSQL-Databases-and-Servers.html
我的主数据库将使用动态 ip 提供程序(我可以在 postgresql 设置中添加动态 ip 地址,例如 myhomedb.dedyn.io 吗?)
我说这一切是为了防止出现任何问题。我的家庭数据库将仅用于更新大量数据,但不是关键任务(因为我们知道云计算并不便宜)。
提前致谢。
看起来 postgres-fdw
是要走的路:https://www.postgresql.org/docs/current/postgres-fdw.html
First install the extension:
CREATE EXTENSION postgres_fdw;
Then create a foreign server using CREATE SERVER
. In this example we
wish to connect to a PostgreSQL server on host 192.83.123.89
listening on port 5432
. The database to which the connection is made
is named foreign_db
on the remote server:
CREATE SERVER foreign_server
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host '192.83.123.89', port '5432', dbname 'foreign_db');
A user mapping, defined with CREATE USER MAPPING
, is needed as well
to identify the role that will be used on the remote server:
CREATE USER MAPPING FOR local_user
SERVER foreign_server
OPTIONS (user 'foreign_user', password 'password');
Now it is possible to create a foreign table with CREATE FOREIGN
TABLE
. In this example we wish to access the table named
some_schema.some_table
on the remote server. The local name for it
will be foreign_table
:
CREATE FOREIGN TABLE foreign_table (
id integer NOT NULL,
data text
)
SERVER foreign_server
OPTIONS (schema_name 'some_schema', table_name 'some_table');
It's essential that the data types and other properties of the columns
declared in CREATE FOREIGN TABLE
match the actual remote table.
Column names must match as well, unless you attach column_name
options
to the individual columns to show how they are named in the remote
table. In many cases, use of IMPORT FOREIGN SCHEMA
is preferable to
constructing foreign table definitions manually.
我家里有一个postgresql数据库,一个在云端。我想将我的家庭数据库添加到云数据库中,以便我可以轻松地在数据库之间进行查询。如何才能做到这一点?不使用 dblink
http://www.postgresonline.com/journal/archives/44-Using-DbLink-to-access-other-PostgreSQL-Databases-and-Servers.html
我的主数据库将使用动态 ip 提供程序(我可以在 postgresql 设置中添加动态 ip 地址,例如 myhomedb.dedyn.io 吗?)
我说这一切是为了防止出现任何问题。我的家庭数据库将仅用于更新大量数据,但不是关键任务(因为我们知道云计算并不便宜)。
提前致谢。
看起来 postgres-fdw
是要走的路:https://www.postgresql.org/docs/current/postgres-fdw.html
First install the extension:
CREATE EXTENSION postgres_fdw;
Then create a foreign server using
CREATE SERVER
. In this example we wish to connect to a PostgreSQL server on host192.83.123.89
listening on port5432
. The database to which the connection is made is namedforeign_db
on the remote server:CREATE SERVER foreign_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '192.83.123.89', port '5432', dbname 'foreign_db');
A user mapping, defined with
CREATE USER MAPPING
, is needed as well to identify the role that will be used on the remote server:CREATE USER MAPPING FOR local_user SERVER foreign_server OPTIONS (user 'foreign_user', password 'password');
Now it is possible to create a foreign table with
CREATE FOREIGN TABLE
. In this example we wish to access the table namedsome_schema.some_table
on the remote server. The local name for it will beforeign_table
:CREATE FOREIGN TABLE foreign_table ( id integer NOT NULL, data text ) SERVER foreign_server OPTIONS (schema_name 'some_schema', table_name 'some_table');
It's essential that the data types and other properties of the columns declared in
CREATE FOREIGN TABLE
match the actual remote table. Column names must match as well, unless you attachcolumn_name
options to the individual columns to show how they are named in the remote table. In many cases, use ofIMPORT FOREIGN SCHEMA
is preferable to constructing foreign table definitions manually.