<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.pgsql.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.pg-connect.php',
    1 => 'pg_connect',
    2 => 'Open a PostgreSQL connection',
  ),
  'up' => 
  array (
    0 => 'ref.pgsql.php',
    1 => 'PostgreSQL Functions',
  ),
  'prev' => 
  array (
    0 => 'function.pg-close.php',
    1 => 'pg_close',
  ),
  'next' => 
  array (
    0 => 'function.pg-connect-poll.php',
    1 => 'pg_connect_poll',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/pgsql/functions/pg-connect.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.pg-connect" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">pg_connect</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">pg_connect</span> &mdash; <span class="dc-title">Open a PostgreSQL connection</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.pg-connect-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>pg_connect</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$connection_string</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span>): <span class="type"><span class="type"><a href="class.pgsql-connection.php" class="type PgSql\Connection">PgSql\Connection</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>pg_connect()</strong></span> opens a connection to a
   PostgreSQL database specified by the
   <code class="parameter">connection_string</code>.
  </p>
  <p class="para">
   If a second call is made to <span class="function"><strong>pg_connect()</strong></span> with
   the same <code class="parameter">connection_string</code> as an existing connection, the
   existing connection will be returned unless you pass
   <strong><code><a href="pgsql.constants.php#constant.pgsql-connect-force-new">PGSQL_CONNECT_FORCE_NEW</a></code></strong> as
   <code class="parameter">flags</code>.
  </p>
  <p class="para">
   The old syntax with multiple parameters
   <strong class="command">$conn = pg_connect(&quot;host&quot;, &quot;port&quot;, &quot;options&quot;, &quot;tty&quot;, &quot;dbname&quot;)
   </strong> has been deprecated.
  </p>
 </div>


<div class="refsect1 parameters" id="refsect1-function.pg-connect-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">connection_string</code></dt>
     <dd>
      <p class="para">
       The <code class="parameter">connection_string</code> can be empty to use all default parameters, or it 
       can contain one or more parameter settings separated by whitespace. 
       Each parameter setting is in the form <code class="literal">keyword = value</code>. Spaces around 
       the equal sign are optional. To write an empty value or a value 
       containing spaces, surround it with single quotes, e.g., <code class="literal">keyword = 
       &#039;a value&#039;</code>. Single quotes and backslashes within the value must be 
       escaped with a backslash, i.e., \&#039; and \\.  
      </p>
      <p class="para">
       The currently recognized parameter keywords are:
       <code class="parameter">host</code>, <code class="parameter">hostaddr</code>, <code class="parameter">port</code>,
       <code class="parameter">dbname</code> (defaults to value of <code class="parameter">user</code>),
       <code class="parameter">user</code>,
       <code class="parameter">password</code>, <code class="parameter">connect_timeout</code>,
       <code class="parameter">options</code>, <code class="parameter">tty</code> (ignored), <code class="parameter">sslmode</code>,
       <code class="parameter">requiressl</code> (deprecated in favor of <code class="parameter">sslmode</code>), and
       <code class="parameter">service</code>.  Which of these arguments exist depends
       on your PostgreSQL version.
      </p>
      <p class="para">
       The <code class="parameter">options</code> parameter can be used to set command line parameters 
       to be invoked by the server.
      </p>
     </dd>
    
    
     <dt><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       If <strong><code><a href="pgsql.constants.php#constant.pgsql-connect-force-new">PGSQL_CONNECT_FORCE_NEW</a></code></strong> is passed, then a new connection
       is created, even if the <code class="parameter">connection_string</code> is identical to
       an existing connection.
      </p>
      <p class="para">
       If <strong><code><a href="pgsql.constants.php#constant.pgsql-connect-async">PGSQL_CONNECT_ASYNC</a></code></strong> is given, then the
       connection is established asynchronously. The state of the connection
       can then be checked via <span class="function"><a href="function.pg-connect-poll.php" class="function">pg_connect_poll()</a></span> or
       <span class="function"><a href="function.pg-connection-status.php" class="function">pg_connection_status()</a></span>.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.pg-connect-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns an <span class="classname"><a href="class.pgsql-connection.php" class="classname">PgSql\Connection</a></span> instance on success,  or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on failure.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.pg-connect-changelog">
  <h3 class="title">Changelog</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.1.0</td>
      <td>
       Returns an <span class="classname"><a href="class.pgsql-connection.php" class="classname">PgSql\Connection</a></span> instance now;
       previously, a <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> was returned.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.pg-connect-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 Using <span class="function"><strong>pg_connect()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$dbconn </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"dbname=mary"</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//connect to a database named "mary"<br /><br /></span><span style="color: #0000BB">$dbconn2 </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"host=localhost port=5432 dbname=mary"</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// connect to a database named "mary" on "localhost" at port "5432"<br /><br /></span><span style="color: #0000BB">$dbconn3 </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"host=sheep port=5432 dbname=mary user=lamb password=foo"</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//connect to a database named "mary" on the host "sheep" with a username and password<br /><br /></span><span style="color: #0000BB">$conn_string </span><span style="color: #007700">= </span><span style="color: #DD0000">"host=sheep port=5432 dbname=test user=lamb password=bar"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$dbconn4 </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn_string</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//connect to a database named "test" on the host "sheep" with a username and password<br /><br /></span><span style="color: #0000BB">$dbconn5 </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"host=localhost options='--client_encoding=UTF8'"</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//connect to a database on "localhost" and set the command line parameter which tells the encoding is in UTF-8<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.pg-connect-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.pg-pconnect.php" class="function" rel="rdfs-seeAlso">pg_pconnect()</a> - Open a persistent PostgreSQL connection</span></li>
    <li><span class="function"><a href="function.pg-close.php" class="function" rel="rdfs-seeAlso">pg_close()</a> - Closes a PostgreSQL connection</span></li>
    <li><span class="function"><a href="function.pg-host.php" class="function" rel="rdfs-seeAlso">pg_host()</a> - Returns the host name associated with the connection</span></li>
    <li><span class="function"><a href="function.pg-port.php" class="function" rel="rdfs-seeAlso">pg_port()</a> - Return the port number associated with the connection</span></li>
    <li><span class="function"><a href="function.pg-tty.php" class="function" rel="rdfs-seeAlso">pg_tty()</a> - Return the TTY name associated with the connection</span></li>
    <li><span class="function"><a href="function.pg-options.php" class="function" rel="rdfs-seeAlso">pg_options()</a> - Get the options associated with the connection</span></li>
    <li><span class="function"><a href="function.pg-dbname.php" class="function" rel="rdfs-seeAlso">pg_dbname()</a> - Get the database name</span></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>