⌘+k ctrl+k
1.3 (稳定版)
搜索快捷键 cmd + k | ctrl + k
PostgreSQL 导入

要直接在运行中的 PostgreSQL 数据库上执行查询,需要 postgres 扩展

安装和加载

该扩展可以使用 INSTALL SQL 命令安装。此操作只需运行一次。

INSTALL postgres;

要加载 postgres 扩展以供使用,请使用 LOAD SQL 命令

LOAD postgres;

用法

安装 postgres 扩展后,可以使用 postgres_scan 函数从 PostgreSQL 查询表。

-- Scan the table "mytable" from the schema "public" in the database "mydb"
SELECT * FROM postgres_scan('host=localhost port=5432 dbname=mydb', 'public', 'mytable');

postgres_scan 函数的第一个参数是 PostgreSQL 连接字符串,它是一个以 {key}={value} 格式提供的连接参数列表。下面是有效参数的列表。

名称 描述 默认值
host 要连接的主机名 localhost
hostaddr 主机 IP 地址 localhost
port 端口号 5432
user PostgreSQL 用户名 [操作系统用户名]
password PostgreSQL 密码  
dbname 数据库名 [用户]
passfile 密码存储文件名 ~/.pgpass

另外,可以使用 ATTACH 命令附加整个数据库。这允许您查询 PostgreSQL 数据库中存储的所有表,就好像它是一个常规数据库一样。

-- Attach the PostgreSQL database using the given connection string
ATTACH 'host=localhost port=5432 dbname=mydb' AS test (TYPE postgres);
-- The table "tbl_name" can now be queried as if it is a regular table
SELECT * FROM test.tbl_name;
-- Switch the active database to "test"
USE test;
-- List all tables in the file
SHOW TABLES;

欲了解更多信息,请参阅 PostgreSQL 扩展文档