CONNECT
Name
CONNECT -- 建立数据库连接
Synopsis
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
,但是 连接参数的格式是具体实施的。