2024年9月26日: PostgreSQL 17 发布!
支持的版本:当前 (17) / 16 / 15 / 14 / 13 / 12
开发版本:devel
不受支持的版本:11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1

CONNECT

CONNECT — 建立数据库连接

概要

CONNECT TO connection_target [ AS connection_name ] [ USER connection_user ]
CONNECT TO DEFAULT
CONNECT connection_user
DATABASE connection_target

描述

CONNECT 命令建立客户端与 PostgreSQL 服务器之间的连接。

参数

connection_target #

connection_target 指定连接的目标服务器,可以使用多种形式。

[ database_name ] [ @host ] [ :port ] #

通过 TCP/IP 连接

unix:postgresql://host [ :port ] / [ database_name ] [ ?connection_option ] #

通过 Unix 域套接字连接

tcp:postgresql://host [ :port ] / [ database_name ] [ ?connection_option ] #

通过 TCP/IP 连接

SQL 字符串常量 #

包含以上形式之一的值

主机变量 #

类型为 char[]VARCHAR[] 的主机变量,包含以上形式之一的值

connection_name #

连接的可选标识符,以便在其他命令中引用它。它可以是 SQL 标识符或主机变量。

connection_user #

数据库连接的用户名称。

此参数还可以使用以下形式之一指定用户名和密码:user_name/passworduser_name IDENTIFIED BY passworduser_name USING password

用户名和密码可以是 SQL 标识符、字符串常量或主机变量。

DEFAULT #

使用 libpq 定义的所有默认连接参数。

示例

这里有一些指定连接参数的不同变体

EXEC SQL CONNECT TO "connectdb" AS main;
EXEC SQL CONNECT TO "connectdb" AS second;
EXEC SQL CONNECT TO "unix:postgresql://200.46.204.71/connectdb" AS main USER connectuser;
EXEC SQL CONNECT TO "unix:postgresql://127.0.0.1/connectdb" AS main USER connectuser;
EXEC SQL CONNECT TO 'connectdb' AS main;
EXEC SQL CONNECT TO 'unix:postgresql://127.0.0.1/connectdb' AS main USER :user;
EXEC SQL CONNECT TO :db AS :id;
EXEC SQL CONNECT TO :db USER connectuser USING :pw;
EXEC SQL CONNECT TO @localhost AS main USER connectdb;
EXEC SQL CONNECT TO REGRESSDB1 as main;
EXEC SQL CONNECT TO AS main USER connectdb;
EXEC SQL CONNECT TO connectdb AS :id;
EXEC SQL CONNECT TO connectdb AS main USER connectuser/connectdb;
EXEC SQL CONNECT TO connectdb AS main;
EXEC SQL CONNECT TO connectdb@localhost AS main;
EXEC SQL CONNECT TO tcp:postgresql://127.0.0.1/ USER connectdb;
EXEC SQL CONNECT TO tcp:postgresql://127.0.0.1/connectdb USER connectuser IDENTIFIED BY connectpw;
EXEC SQL CONNECT TO tcp:postgresql://127.0.0.1:20/connectdb USER connectuser IDENTIFIED BY connectpw;
EXEC SQL CONNECT TO unix:postgresql://127.0.0.1/ AS main USER connectdb;
EXEC SQL CONNECT TO unix:postgresql://127.0.0.1/connectdb AS main USER connectuser;
EXEC SQL CONNECT TO unix:postgresql://127.0.0.1/connectdb USER connectuser IDENTIFIED BY "connectpw";
EXEC SQL CONNECT TO unix:postgresql://127.0.0.1/connectdb USER connectuser USING "connectpw";
EXEC SQL CONNECT TO unix:postgresql://127.0.0.1/connectdb?connect_timeout=14 USER connectuser;

这是一个说明如何使用主机变量指定连接参数的示例程序

int
main(void)
{
EXEC SQL BEGIN DECLARE SECTION;
    char *dbname     = "testdb";    /* database name */
    char *user       = "testuser";  /* connection user name */
    char *connection = "tcp:postgresql://127.0.0.1:5432/testdb";
                                    /* connection string */
    char ver[256];                  /* buffer to store the version string */
EXEC SQL END DECLARE SECTION;

    ECPGdebug(1, stderr);

    EXEC SQL CONNECT TO :dbname USER :user;
    EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT;
    EXEC SQL SELECT version() INTO :ver;
    EXEC SQL DISCONNECT;

    printf("version: %s\n", ver);

    EXEC SQL CONNECT TO :connection USER :user;
    EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT;
    EXEC SQL SELECT version() INTO :ver;
    EXEC SQL DISCONNECT;

    printf("version: %s\n", ver);

    return 0;
}

兼容性

CONNECT 在 SQL 标准中指定,但连接参数的格式是特定于实现的。

另请参阅

DISCONNECTSET CONNECTION

提交更正

如果您在文档中发现任何不正确的内容、与您对特定功能的体验不符的内容或需要进一步澄清的内容,请使用此表单 报告文档问题。