4 CONNECT — establish a database connection
8 CONNECT TO connection_target [ AS connection_name ] [ USER connection_user ]
10 CONNECT connection_user
11 DATABASE connection_target
15 The CONNECT command establishes a connection between the client and the
21 connection_target specifies the target server of the connection
22 on one of several forms.
24 [ database_name ] [ @host ] [ :port ] #
27 unix:postgresql://host [ :port ] / [ database_name ] [
28 ?connection_option ] #
29 Connect over Unix-domain sockets
31 tcp:postgresql://host [ :port ] / [ database_name ] [
32 ?connection_option ] #
36 containing a value in one of the above forms
39 host variable of type char[] or VARCHAR[] containing a
40 value in one of the above forms
43 An optional identifier for the connection, so that it can be
44 referred to in other commands. This can be an SQL identifier or
48 The user name for the database connection.
50 This parameter can also specify user name and password, using
51 one the forms user_name/password, user_name IDENTIFIED BY
52 password, or user_name USING password.
54 User name and password can be SQL identifiers, string constants,
58 Use all default connection parameters, as defined by libpq.
62 Here a several variants for specifying connection parameters:
63 EXEC SQL CONNECT TO "connectdb" AS main;
64 EXEC SQL CONNECT TO "connectdb" AS second;
65 EXEC SQL CONNECT TO "unix:postgresql://200.46.204.71/connectdb" AS main USER con
67 EXEC SQL CONNECT TO "unix:postgresql://localhost/connectdb" AS main USER connect
69 EXEC SQL CONNECT TO 'connectdb' AS main;
70 EXEC SQL CONNECT TO 'unix:postgresql://localhost/connectdb' AS main USER :user;
71 EXEC SQL CONNECT TO :db AS :id;
72 EXEC SQL CONNECT TO :db USER connectuser USING :pw;
73 EXEC SQL CONNECT TO @localhost AS main USER connectdb;
74 EXEC SQL CONNECT TO REGRESSDB1 as main;
75 EXEC SQL CONNECT TO AS main USER connectdb;
76 EXEC SQL CONNECT TO connectdb AS :id;
77 EXEC SQL CONNECT TO connectdb AS main USER connectuser/connectdb;
78 EXEC SQL CONNECT TO connectdb AS main;
79 EXEC SQL CONNECT TO connectdb@localhost AS main;
80 EXEC SQL CONNECT TO tcp:postgresql://localhost/ USER connectdb;
81 EXEC SQL CONNECT TO tcp:postgresql://localhost/connectdb USER connectuser IDENTI
83 EXEC SQL CONNECT TO tcp:postgresql://localhost:20/connectdb USER connectuser IDE
85 EXEC SQL CONNECT TO unix:postgresql://localhost/ AS main USER connectdb;
86 EXEC SQL CONNECT TO unix:postgresql://localhost/connectdb AS main USER connectus
88 EXEC SQL CONNECT TO unix:postgresql://localhost/connectdb USER connectuser IDENT
90 EXEC SQL CONNECT TO unix:postgresql://localhost/connectdb USER connectuser USING
92 EXEC SQL CONNECT TO unix:postgresql://localhost/connectdb?connect_timeout=14 USE
95 Here is an example program that illustrates the use of host variables
96 to specify connection parameters:
100 EXEC SQL BEGIN DECLARE SECTION;
101 char *dbname = "testdb"; /* database name */
102 char *user = "testuser"; /* connection user name */
103 char *connection = "tcp:postgresql://localhost:5432/testdb";
104 /* connection string */
105 char ver[256]; /* buffer to store the version string */
106 EXEC SQL END DECLARE SECTION;
108 ECPGdebug(1, stderr);
110 EXEC SQL CONNECT TO :dbname USER :user;
111 EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL CO
113 EXEC SQL SELECT version() INTO :ver;
116 printf("version: %s\n", ver);
118 EXEC SQL CONNECT TO :connection USER :user;
119 EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL CO
121 EXEC SQL SELECT version() INTO :ver;
124 printf("version: %s\n", ver);
131 CONNECT is specified in the SQL standard, but the format of the
132 connection parameters is implementation-specific.
136 DISCONNECT, SET CONNECTION