如何在 PostgreSQL 中使用(安装)dblink?

发布于 2024-09-26 07:12:28 字数 747 浏览 9 评论 0 原文

我习惯了 Oracle 并在我的架构中创建 dblink,然后访问远程数据库,如下所示:mytable@myremotedb,是否有对 PostgreSQL 做同样的事情?

现在我正在使用 dblink,如下所示:

SELECT logindate FROM dblink('host=dev.toto.com
                              user=toto
                              password=isok
                              dbname=totofamily', 'SELECT logindate FROM loginlog');

当我执行此命令时,出现以下错误:

提示:没有函数与给定的名称和参数类型匹配。您可能需要添加显式类型转换。

有人有想法吗?在使用dblinks之前我们是否必须“激活”它们或者做一些事情?

我们要查询的远程数据库有什么要做的吗?我们还必须激活 dblink 吗?我一直遇到无法建立连接。该行的类型为:

SELECT dblink_connect_u('host=x.x.x.x dbname=mydb user=root port=5432');

IP 地址正确并且 Postgres 正在远程服务器上运行。有什么想法吗?

I am used to Oracle and to create a dblink in my schema and then access to a remote database like this: mytable@myremotedb, is there anyway do to the same with PostgreSQL?

Right now I am using dblink like this:

SELECT logindate FROM dblink('host=dev.toto.com
                              user=toto
                              password=isok
                              dbname=totofamily', 'SELECT logindate FROM loginlog');

When I execute this command I get the following error:

HINT: No function matches the given name and argument types. You might need to add explicit type casts.

Does anybody have an idea ? Do we have to "activate" dblinks or do something before using them?

Is there something to do on the remote database we are going to query? Do we have to activate dblink too? I keep having a could not establish connection. This is the line is type:

SELECT dblink_connect_u('host=x.x.x.x dbname=mydb user=root port=5432');

IP Address is correct and Postgres is running on the remote server. Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

把人绕傻吧 2024-10-03 07:12:28

PostgreSQL 9.1开始,附加模块的安装很简单。 可以使用dblink等注册扩展href="https://www.postgresql.org/docs/current/sql-createextension.html" rel="noreferrer">CREATE EXTENSION

CREATE EXTENSION dblink;

安装到您的默认架构中,该架构默认情况下是public。在运行命令之前,请确保您的 search_path 设置正确。该架构必须对所有使用该架构的角色可见。请参阅:

或者,您可以安装到您选择的任何架构:

CREATE EXTENSION dblink SCHEMA extensions;

请参阅:

每个数据库运行一次。或者在标准系统数据库 template1 中运行它,将其自动添加到每个新创建的数据库中。 手册中的详细信息。

您需要安装提供模块的文件首先在服务器上。对于 Debian 及其衍生版本,这将是软件包 postgresql-contrib-9.1 - 对于 PostgreSQL 9.1,明显地。从 Postgres 10 开始,只有一个 postgresql-contrib 元包。

Since PostgreSQL 9.1, installation of additional modules is simple. Registered extensions like dblink can be installed with CREATE EXTENSION:

CREATE EXTENSION dblink;

Installs into your default schema, which is public by default. Make sure your search_path is set properly before you run the command. The schema must be visible to all roles who have to work with it. See:

Alternatively, you can install to any schema of your choice with:

CREATE EXTENSION dblink SCHEMA extensions;

See:

Run once per database. Or run it in the standard system database template1 to add it to every newly created DB automatically. Details in the manual.

You need to have the files providing the module installed on the server first. For Debian and derivatives this would be the package postgresql-contrib-9.1 - for PostgreSQL 9.1, obviously. Since Postgres 10, there is just a postgresql-contrib metapackage.

你又不是我 2024-10-03 07:12:28

我正在使用 DBLINK 连接内部数据库以进行跨数据库查询。

参考本文。

安装DbLink扩展。

CREATE EXTENSION dblink;

验证DbLink:

SELECT pg_namespace.nspname, pg_proc.proname 
FROM pg_proc, pg_namespace 
WHERE pg_proc.pronamespace=pg_namespace.oid 
   AND pg_proc.proname LIKE '%dblink%';

测试数据库连接:

SELECT dblink_connect('host=localhost user=postgres password=enjoy dbname=postgres');

I am using DBLINK to connect internal database for cross database queries.

Reference taken from this article.

Install DbLink extension.

CREATE EXTENSION dblink;

Verify DbLink:

SELECT pg_namespace.nspname, pg_proc.proname 
FROM pg_proc, pg_namespace 
WHERE pg_proc.pronamespace=pg_namespace.oid 
   AND pg_proc.proname LIKE '%dblink%';

Test connection of database:

SELECT dblink_connect('host=localhost user=postgres password=enjoy dbname=postgres');
情仇皆在手 2024-10-03 07:12:28

在 Linux 上,找到 dblink.sql,然后在 postgresql 控制台中执行类似以下内容以创建所有必需的函数:

\i /usr/share/postgresql/8.4/contrib/dblink.sql 

您可能需要安装 contrib 软件包: sudo apt-get install postgresql-contrib

On linux, find dblink.sql, then execute in the postgresql console something like this to create all required functions:

\i /usr/share/postgresql/8.4/contrib/dblink.sql 

you might need to install the contrib packages: sudo apt-get install postgresql-contrib

烟酉 2024-10-03 07:12:28

安装模块通常需要您运行数据库安装中包含的 SQL 脚本。

假设类似 linux 的操作系统

find / -name dblink.sql

验证位置并运行它

Installing modules usually requires you to run an sql script that is included with the database installation.

Assuming linux-like OS

find / -name dblink.sql

Verify the location and run it

你对谁都笑 2024-10-03 07:12:28

可以通过使用以下方式添加:

$psql -d databaseName -c "CREATE EXTENSION dblink"

It can be added by using:

$psql -d databaseName -c "CREATE EXTENSION dblink"
清眉祭 2024-10-03 07:12:28
# or even faster copy paste answer if you have sudo on the host 
sudo su - postgres  -c "psql template1 -c 'CREATE EXTENSION IF NOT EXISTS \"dblink\";'"
# or even faster copy paste answer if you have sudo on the host 
sudo su - postgres  -c "psql template1 -c 'CREATE EXTENSION IF NOT EXISTS \"dblink\";'"
秋叶绚丽 2024-10-03 07:12:28

另外你还会问,dblink安装在哪里?

在大多数情况下,建议安装常见 Postgers 扩展的位置是 pg_catalog 架构。如果您在 pg_catalog 中安装扩展,则所有相关函数在任何模式中都可用。如果您在 public 中安装扩展,那么在另一个模式中,您还必须定义模式。

例如,如果 dblink 安装在 public 中,则在架构 myschema 中,您必须定义 public 架构。

SELECT public.dblink_get_connections();

例如,如果 dblink 安装在 pg_catalog 中,则在方案 myschema 中,您不必定义 public schema:

SELECT dblink_get_connections();

schema pg_catalog 中的所有函数都会自动附加到其他 schema。

要在 pg_catalog 中安装 dblink

CREATE EXTENSION IF NOT EXISTS dblink WITH SCHEMA pg_catalog;

You shall ask additionally, where to install dblink?

On most cases recommended location where to install common Postgers extensions is pg_catalog schema. If you install extensions in pg_catalog, then all related functions are available within any schema. If you install extension in public, then inside another schema, you have to define also schema.

For example, if dblink is installed in public, then within schema myschema, you have to define public schema.

SELECT public.dblink_get_connections();

For example, if dblink is installed in pg_catalog, then within scheme myschema, you do not have to define public schema:

SELECT dblink_get_connections();

All functions in schema pg_catalog are appended to other schemas automatically.

To install dblink in pg_catalog:

CREATE EXTENSION IF NOT EXISTS dblink WITH SCHEMA pg_catalog;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文