<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Open a PostgreSQL connection</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.pg-connect-poll.html">pg_connect_poll</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.pg-connection-busy.html">pg_connection_busy</a></div> <div class="up"><a href="ref.pgsql.html">PostgreSQL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><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)</p><p class="refpurpose"><span class="refname">pg_connect</span> — <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="type">resource</span> <span class="methodname"><strong>pg_connect</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$connection_string</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$connect_type</code></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>PGSQL_CONNECT_FORCE_NEW</code></strong> as <code class="parameter">connect_type</code>. </p> <p class="para"> The old syntax with multiple parameters <strong class="command">$conn = pg_connect("host", "port", "options", "tty", "dbname") </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 <em>keyword = value</em>. Spaces around the equal sign are optional. To write an empty value or a value containing spaces, surround it with single quotes, e.g., <em>keyword = 'a value'</em>. Single quotes and backslashes within the value must be escaped with a backslash, i.e., \' 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">connect_type</code></dt> <dd> <p class="para"> If <strong><code>PGSQL_CONNECT_FORCE_NEW</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>PGSQL_CONNECT_ASYNC</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.html" class="function">pg_connect_poll()</a></span> or <span class="function"><a href="function.pg-connection-status.html" 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"> PostgreSQL connection resource on success, <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 changelog" id="refsect1-function.pg-connect-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.6.0</td> <td> Support for giving the <strong><code>PGSQL_CONNECT_ASYNC</code></strong> constant as the <code class="parameter">connect_type</code> was added. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.pg-connect-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2528"> <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"><?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">?></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 class="member"><span class="function"><a href="function.pg-pconnect.html" class="function" rel="rdfs-seeAlso">pg_pconnect()</a> - Open a persistent PostgreSQL connection</span></li> <li class="member"><span class="function"><a href="function.pg-close.html" class="function" rel="rdfs-seeAlso">pg_close()</a> - Closes a PostgreSQL connection</span></li> <li class="member"><span class="function"><a href="function.pg-host.html" class="function" rel="rdfs-seeAlso">pg_host()</a> - Returns the host name associated with the connection</span></li> <li class="member"><span class="function"><a href="function.pg-port.html" class="function" rel="rdfs-seeAlso">pg_port()</a> - Return the port number associated with the connection</span></li> <li class="member"><span class="function"><a href="function.pg-tty.html" class="function" rel="rdfs-seeAlso">pg_tty()</a> - Return the TTY name associated with the connection</span></li> <li class="member"><span class="function"><a href="function.pg-options.html" class="function" rel="rdfs-seeAlso">pg_options()</a> - Get the options associated with the connection</span></li> <li class="member"><span class="function"><a href="function.pg-dbname.html" class="function" rel="rdfs-seeAlso">pg_dbname()</a> - Get the database name</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.pg-connect-poll.html">pg_connect_poll</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.pg-connection-busy.html">pg_connection_busy</a></div> <div class="up"><a href="ref.pgsql.html">PostgreSQL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>