问题报告 纠错本页面

CONNECT

名称

CONNECT -- 建立数据库连接

大纲

CONNECT TO connection_target [ AS connection_name ] [ USER connection_user_name ]
CONNECT TO DEFAULT
CONNECT connection_user_name
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_object

用于连接可选标识符,这样就可以指向其他命令。 这可以是一个SQL标识符或者宿主变量。

connection_user

用于数据库连接的用户名。

该参数可以指定用户名和密码,使用下面形式之一 user_name/password, user_name IDENTIFIED BY password或者 user_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://localhost/connectdb" AS main USER connectuser;
EXEC SQL CONNECT TO 'connectdb' AS main;
EXEC SQL CONNECT TO 'unix:postgresql://localhost/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://localhost/ USER connectdb;
EXEC SQL CONNECT TO tcp:postgresql://localhost/connectdb USER connectuser IDENTIFIED BY connectpw;
EXEC SQL CONNECT TO tcp:postgresql://localhost:20/connectdb USER connectuser IDENTIFIED BY connectpw;
EXEC SQL CONNECT TO unix:postgresql://localhost/ AS main USER connectdb;
EXEC SQL CONNECT TO unix:postgresql://localhost/connectdb AS main USER connectuser;
EXEC SQL CONNECT TO unix:postgresql://localhost/connectdb USER connectuser IDENTIFIED BY "connectpw";
EXEC SQL CONNECT TO unix:postgresql://localhost/connectdb USER connectuser USING "connectpw";
EXEC SQL CONNECT TO unix:postgresql://localhost/connectdb?connect_timeout=14 USER connectuser;

这里有一个例子程序阐述了使用宿主变量指定连接参数:

int
main(void)
{
EXEC SQL BEGIN DECLARE SECTION;

    

char *dbname     = "testdb";    /*数据库名称*/
    char *user       = "testuser";  /*连接用户名*/
    char *connection = "tcp:postgresql://localhost:5432/testdb";
                                    /* 连接字符串 */
    char ver[256];                  /*存储版本字符串的缓冲区*/
EXEC SQL END DECLARE SECTION;


    ECPGdebug(1, stderr);

    EXEC SQL CONNECT TO :dbname USER :user;
    EXEC SQL SELECT version() INTO :ver;
    EXEC SQL DISCONNECT;

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

    EXEC SQL CONNECT TO :connection USER :user;
    EXEC SQL SELECT version() INTO :ver;
    EXEC SQL DISCONNECT;

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

    return 0;
}

兼容性

在SQL标准中声明CONNECT,但是 连接参数的格式是具体实施的。

参见

DISCONNECT, SET CONNECTION