????
Current Path : C:/opt/pgsql/doc/postgresql/html/ |
Current File : C:/opt/pgsql/doc/postgresql/html/ecpg-sql-connect.html |
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!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="36.14. Embedded SQL Commands">Up</a></td><th width="60%" align="center">36.14. Embedded SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 16.3 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"> 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> ] CONNECT TO DEFAULT CONNECT <em class="replaceable"><code>connection_user</code></em> DATABASE <em class="replaceable"><code>connection_target</code></em> </pre></div><div class="refsect1" id="id-1.7.5.20.4.3"><h2>Description</h2><p> The <code class="command">CONNECT</code> command establishes a connection between the client and the PostgreSQL server. </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> <em class="replaceable"><code>connection_target</code></em> specifies the target server of the connection on one of several forms. </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> Connect over TCP/IP </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> Connect over Unix-domain sockets </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> Connect over TCP/IP </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> containing a value in one of the above forms </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> host variable of type <code class="type">char[]</code> or <code class="type">VARCHAR[]</code> containing a value in one of the above forms </p></dd></dl></div><p> </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> 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. </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> The user name for the database connection. </p><p> This parameter can also specify user name and password, using one the forms <code class="literal"><em class="replaceable"><code>user_name</code></em>/<em class="replaceable"><code>password</code></em></code>, <code class="literal"><em class="replaceable"><code>user_name</code></em> IDENTIFIED BY <em class="replaceable"><code>password</code></em></code>, or <code class="literal"><em class="replaceable"><code>user_name</code></em> USING <em class="replaceable"><code>password</code></em></code>. </p><p> User name and password can be SQL identifiers, string constants, or host variables. </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> Use all default connection parameters, as defined by libpq. </p></dd></dl></div></div><div class="refsect1" id="id-1.7.5.20.4.5"><h2>Examples</h2><p> Here a several variants for specifying connection parameters: </p><pre class="programlisting"> 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; </pre><p> </p><p> Here is an example program that illustrates the use of host variables to specify connection parameters: </p><pre class="programlisting"> int main(void) { EXEC SQL BEGIN DECLARE SECTION; char *dbname = "testdb"; /* database name */ char *user = "testuser"; /* connection user name */ char *connection = "tcp:postgresql://localhost:5432/testdb"; /* connection string */ char ver[256]; /* buffer to store the version string */ EXEC SQL END DECLARE SECTION; ECPGdebug(1, stderr); EXEC SQL CONNECT TO :dbname USER :user; EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT; EXEC SQL SELECT version() INTO :ver; EXEC SQL DISCONNECT; printf("version: %s\n", ver); EXEC SQL CONNECT TO :connection USER :user; EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT; EXEC SQL SELECT version() INTO :ver; EXEC SQL DISCONNECT; printf("version: %s\n", ver); return 0; } </pre><p> </p></div><div class="refsect1" id="id-1.7.5.20.4.6"><h2>Compatibility</h2><p> <code class="command">CONNECT</code> is specified in the SQL standard, but the format of the connection parameters is implementation-specific. </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="36.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 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> DEALLOCATE DESCRIPTOR</td></tr></table></div></body></html>