Name
CONNECT -- establish a database connection
Synopsis
CONNECT TO connection_target [ AS connection_name ] [ USER connection_user_name ]
CONNECT TO DEFAULT
CONNECT connection_user_name
DATABASE connection_target
Description
The CONNECT command establishes a connection between the client and the PostgreSQL server.
Parameters
connection_target specifies the target server of the connection on one of several forms.
Connect over TCP/IP
Connect over Unix-domain sockets
Connect over TCP/IP
containing a value in one of the above forms
host variable of fixed-length string (trailing spaces are ignored) containing a value in one of the above forms
An optional identifier for the connection, so that it can be referred to in other commands. This can be an SQL identifier or a host variable.
The user name for the database connection.
This parameter can also specify user name and password, using one the forms user_name/password, user_name IDENTIFIED BY password, or user_name USING password.
User name and password can be SQL identifiers, string constants, or host variables(fixed-length string, trailing spaces are ignored).
Use all default connection parameters, as defined by libpq.
Examples
Here a several variants for specifying connection parameters:
EXEC SQL CONNECT TO "connectdb" AS main END-EXEC. EXEC SQL CONNECT TO "connectdb" AS second END-EXEC. EXEC SQL CONNECT TO "unix:postgresql://localhost/connectdb" AS main USER connectuser END-EXEC. EXEC SQL CONNECT TO 'connectdb' AS main END-EXEC. EXEC SQL CONNECT TO 'unix:postgresql://localhost/connectdb' AS main USER :user END-EXEC. EXEC SQL CONNECT TO :dbn AS :idt END-EXEC. EXEC SQL CONNECT TO :dbn USER connectuser USING :pw END-EXEC. EXEC SQL CONNECT TO @localhost AS main USER connectdb END-EXEC. EXEC SQL CONNECT TO REGRESSDB1 as main END-EXEC. EXEC SQL CONNECT TO connectdb AS :idt END-EXEC. EXEC SQL CONNECT TO connectdb AS main USER connectuser/connectdb END-EXEC. EXEC SQL CONNECT TO connectdb AS main END-EXEC. EXEC SQL CONNECT TO connectdb@localhost AS main END-EXEC. EXEC SQL CONNECT TO tcp:postgresql://localhost/ USER connectdb END-EXEC. EXEC SQL CONNECT TO tcp:postgresql://localhost/connectdb USER connectuser IDENTIFIED BY connectpw END-EXEC. EXEC SQL CONNECT TO tcp:postgresql://localhost:20/connectdb USER connectuser IDENTIFIED BY connectpw END-EXEC. EXEC SQL CONNECT TO unix:postgresql://localhost/ AS main USER connectdb END-EXEC. EXEC SQL CONNECT TO unix:postgresql://localhost/connectdb AS main USER connectuser END-EXEC. EXEC SQL CONNECT TO unix:postgresql://localhost/connectdb USER connectuser IDENTIFIED BY "connectpw" END-EXEC. EXEC SQL CONNECT TO unix:postgresql://localhost/connectdb USER connectuser USING "connectpw" END-EXEC. EXEC SQL CONNECT TO unix:postgresql://localhost/connectdb?connect_timeout=14 USER connectuser END-EXEC.
Here is an example program that illustrates the use of host variables to specify connection parameters:
EXEC SQL BEGIN DECLARE SECTION END-EXEC. * database name 01 DBNAME PIC X(6). * connection user name 01 USER PIC X(8). * connection string 01 CONNECTION PIC X(38). 01 VER PIC X(256). EXEC SQL END DECLARE SECTION END-EXEC. MOVE "testdb" TO DBNAME. MOVE "testuser" TO USER. MOVE "tcp:postgresql://localhost:5432/testdb" TO CONNECTION. EXEC SQL CONNECT TO :DBNAME USER :USER END-EXEC. EXEC SQL SELECT version() INTO :VER END-EXEC. EXEC SQL DISCONNECT END-EXEC. DISPLAY "version: " VER. EXEC SQL CONNECT TO :CONNECTION USER :USER END-EXEC. EXEC SQL SELECT version() INTO :VER END-EXEC. EXEC SQL DISCONNECT END-EXEC. DISPLAY "version: " VER.
Compatibility
CONNECT is specified in the SQL standard, but the format of the connection parameters is implementation-specific.
See Also