]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/ecpg-sql-connect.html
PG 18 docs from https://ftp.postgresql.org/pub/source/v18.0/postgresql-18.0-docs...
[ai-pg] / full-docs / src / sgml / html / ecpg-sql-connect.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>CONNECT</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="ecpg-sql-allocate-descriptor.html" title="ALLOCATE DESCRIPTOR" /><link rel="next" href="ecpg-sql-deallocate-descriptor.html" title="DEALLOCATE DESCRIPTOR" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">CONNECT</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="ecpg-sql-allocate-descriptor.html" title="ALLOCATE DESCRIPTOR">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="ecpg-sql-commands.html" title="34.14. Embedded SQL Commands">Up</a></td><th width="60%" align="center">34.14. Embedded SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="ecpg-sql-deallocate-descriptor.html" title="DEALLOCATE DESCRIPTOR">Next</a></td></tr></table><hr /></div><div class="refentry" id="ECPG-SQL-CONNECT"><div class="titlepage"></div><div class="refnamediv"><h2>CONNECT</h2><p>CONNECT — establish a database connection</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
3 CONNECT TO <em class="replaceable"><code>connection_target</code></em> [ AS <em class="replaceable"><code>connection_name</code></em> ] [ USER <em class="replaceable"><code>connection_user</code></em> ]
4 CONNECT TO DEFAULT
5 CONNECT <em class="replaceable"><code>connection_user</code></em>
6 DATABASE <em class="replaceable"><code>connection_target</code></em>
7 </pre></div><div class="refsect1" id="id-1.7.5.20.4.3"><h2>Description</h2><p>
8      The <code class="command">CONNECT</code> command establishes a connection
9      between the client and the PostgreSQL server.
10     </p></div><div class="refsect1" id="id-1.7.5.20.4.4"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt id="ECPG-SQL-CONNECT-CONNECTION-TARGET"><span class="term"><em class="replaceable"><code>connection_target</code></em></span> <a href="#ECPG-SQL-CONNECT-CONNECTION-TARGET" class="id_link">#</a></dt><dd><p>
11         <em class="replaceable"><code>connection_target</code></em>
12         specifies the target server of the connection on one of
13         several forms.
14
15         </p><div class="variablelist"><dl class="variablelist"><dt id="ECPG-SQL-CONNECT-CONNECTION-TARGET-DATABASE-NAME"><span class="term">[ <em class="replaceable"><code>database_name</code></em> ] [ <code class="literal">@</code><em class="replaceable"><code>host</code></em> ] [ <code class="literal">:</code><em class="replaceable"><code>port</code></em> ]</span> <a href="#ECPG-SQL-CONNECT-CONNECTION-TARGET-DATABASE-NAME" class="id_link">#</a></dt><dd><p>
16             Connect over TCP/IP
17            </p></dd><dt id="ECPG-SQL-CONNECT-CONNECTION-TARGET-UNIX-DOMAIN-SOCKETS"><span class="term"><code class="literal">unix:postgresql://</code><em class="replaceable"><code>host</code></em> [ <code class="literal">:</code><em class="replaceable"><code>port</code></em> ] <code class="literal">/</code> [ <em class="replaceable"><code>database_name</code></em> ] [ <code class="literal">?</code><em class="replaceable"><code>connection_option</code></em> ]</span> <a href="#ECPG-SQL-CONNECT-CONNECTION-TARGET-UNIX-DOMAIN-SOCKETS" class="id_link">#</a></dt><dd><p>
18             Connect over Unix-domain sockets
19            </p></dd><dt id="ECPG-SQL-CONNECT-CONNECTION-TARGET-TCP-IP"><span class="term"><code class="literal">tcp:postgresql://</code><em class="replaceable"><code>host</code></em> [ <code class="literal">:</code><em class="replaceable"><code>port</code></em> ] <code class="literal">/</code> [ <em class="replaceable"><code>database_name</code></em> ] [ <code class="literal">?</code><em class="replaceable"><code>connection_option</code></em> ]</span> <a href="#ECPG-SQL-CONNECT-CONNECTION-TARGET-TCP-IP" class="id_link">#</a></dt><dd><p>
20             Connect over TCP/IP
21            </p></dd><dt id="ECPG-SQL-CONNECT-CONNECTION-TARGET-CONSTANT"><span class="term">SQL string constant</span> <a href="#ECPG-SQL-CONNECT-CONNECTION-TARGET-CONSTANT" class="id_link">#</a></dt><dd><p>
22             containing a value in one of the above forms
23            </p></dd><dt id="ECPG-SQL-CONNECT-CONNECTION-TARGET-HOST-VARIABLE"><span class="term">host variable</span> <a href="#ECPG-SQL-CONNECT-CONNECTION-TARGET-HOST-VARIABLE" class="id_link">#</a></dt><dd><p>
24             host variable of type <code class="type">char[]</code>
25             or <code class="type">VARCHAR[]</code> containing a value in one of the
26             above forms
27            </p></dd></dl></div><p>
28        </p></dd><dt id="ECPG-SQL-CONNECT-CONNECTION-NAME"><span class="term"><em class="replaceable"><code>connection_name</code></em></span> <a href="#ECPG-SQL-CONNECT-CONNECTION-NAME" class="id_link">#</a></dt><dd><p>
29         An optional identifier for the connection, so that it can be
30         referred to in other commands.  This can be an SQL identifier
31         or a host variable.
32        </p></dd><dt id="ECPG-SQL-CONNECT-CONNECTION-USER"><span class="term"><em class="replaceable"><code>connection_user</code></em></span> <a href="#ECPG-SQL-CONNECT-CONNECTION-USER" class="id_link">#</a></dt><dd><p>
33         The user name for the database connection.
34        </p><p>
35         This parameter can also specify user name and password, using one the forms
36         <code class="literal"><em class="replaceable"><code>user_name</code></em>/<em class="replaceable"><code>password</code></em></code>,
37         <code class="literal"><em class="replaceable"><code>user_name</code></em> IDENTIFIED BY <em class="replaceable"><code>password</code></em></code>, or
38         <code class="literal"><em class="replaceable"><code>user_name</code></em> USING <em class="replaceable"><code>password</code></em></code>.
39        </p><p>
40         User name and password can be SQL identifiers, string
41         constants, or host variables.
42        </p></dd><dt id="ECPG-SQL-CONNECT-DEFAULT"><span class="term"><code class="literal">DEFAULT</code></span> <a href="#ECPG-SQL-CONNECT-DEFAULT" class="id_link">#</a></dt><dd><p>
43         Use all default connection parameters, as defined by libpq.
44        </p></dd></dl></div></div><div class="refsect1" id="id-1.7.5.20.4.5"><h2>Examples</h2><p>
45      Here a several variants for specifying connection parameters:
46 </p><pre class="programlisting">
47 EXEC SQL CONNECT TO "connectdb" AS main;
48 EXEC SQL CONNECT TO "connectdb" AS second;
49 EXEC SQL CONNECT TO "unix:postgresql://200.46.204.71/connectdb" AS main USER connectuser;
50 EXEC SQL CONNECT TO "unix:postgresql://localhost/connectdb" AS main USER connectuser;
51 EXEC SQL CONNECT TO 'connectdb' AS main;
52 EXEC SQL CONNECT TO 'unix:postgresql://localhost/connectdb' AS main USER :user;
53 EXEC SQL CONNECT TO :db AS :id;
54 EXEC SQL CONNECT TO :db USER connectuser USING :pw;
55 EXEC SQL CONNECT TO @localhost AS main USER connectdb;
56 EXEC SQL CONNECT TO REGRESSDB1 as main;
57 EXEC SQL CONNECT TO AS main USER connectdb;
58 EXEC SQL CONNECT TO connectdb AS :id;
59 EXEC SQL CONNECT TO connectdb AS main USER connectuser/connectdb;
60 EXEC SQL CONNECT TO connectdb AS main;
61 EXEC SQL CONNECT TO connectdb@localhost AS main;
62 EXEC SQL CONNECT TO tcp:postgresql://localhost/ USER connectdb;
63 EXEC SQL CONNECT TO tcp:postgresql://localhost/connectdb USER connectuser IDENTIFIED BY connectpw;
64 EXEC SQL CONNECT TO tcp:postgresql://localhost:20/connectdb USER connectuser IDENTIFIED BY connectpw;
65 EXEC SQL CONNECT TO unix:postgresql://localhost/ AS main USER connectdb;
66 EXEC SQL CONNECT TO unix:postgresql://localhost/connectdb AS main USER connectuser;
67 EXEC SQL CONNECT TO unix:postgresql://localhost/connectdb USER connectuser IDENTIFIED BY "connectpw";
68 EXEC SQL CONNECT TO unix:postgresql://localhost/connectdb USER connectuser USING "connectpw";
69 EXEC SQL CONNECT TO unix:postgresql://localhost/connectdb?connect_timeout=14 USER connectuser;
70 </pre><p>
71     </p><p>
72      Here is an example program that illustrates the use of host
73      variables to specify connection parameters:
74 </p><pre class="programlisting">
75 int
76 main(void)
77 {
78 EXEC SQL BEGIN DECLARE SECTION;
79     char *dbname     = "testdb";    /* database name */
80     char *user       = "testuser";  /* connection user name */
81     char *connection = "tcp:postgresql://localhost:5432/testdb";
82                                     /* connection string */
83     char ver[256];                  /* buffer to store the version string */
84 EXEC SQL END DECLARE SECTION;
85
86     ECPGdebug(1, stderr);
87
88     EXEC SQL CONNECT TO :dbname USER :user;
89     EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT;
90     EXEC SQL SELECT version() INTO :ver;
91     EXEC SQL DISCONNECT;
92
93     printf("version: %s\n", ver);
94
95     EXEC SQL CONNECT TO :connection USER :user;
96     EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT;
97     EXEC SQL SELECT version() INTO :ver;
98     EXEC SQL DISCONNECT;
99
100     printf("version: %s\n", ver);
101
102     return 0;
103 }
104 </pre><p>
105     </p></div><div class="refsect1" id="id-1.7.5.20.4.6"><h2>Compatibility</h2><p>
106      <code class="command">CONNECT</code> is specified in the SQL standard, but
107      the format of the connection parameters is
108      implementation-specific.
109     </p></div><div class="refsect1" id="id-1.7.5.20.4.7"><h2>See Also</h2><span class="simplelist"><a class="xref" href="ecpg-sql-disconnect.html" title="DISCONNECT">DISCONNECT</a>, <a class="xref" href="ecpg-sql-set-connection.html" title="SET CONNECTION">SET CONNECTION</a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ecpg-sql-allocate-descriptor.html" title="ALLOCATE DESCRIPTOR">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ecpg-sql-commands.html" title="34.14. Embedded SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ecpg-sql-deallocate-descriptor.html" title="DEALLOCATE DESCRIPTOR">Next</a></td></tr><tr><td width="40%" align="left" valign="top">ALLOCATE DESCRIPTOR </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> DEALLOCATE DESCRIPTOR</td></tr></table></div></body></html>