(PHP 4, PHP 5)
pg_connect — 打开一个 PostgreSQL 连接
$connection_string
)pg_connect() 返回其它 PostgreSQL 函数所需要的资源。
pg_connect() 打开一个由
connection_string
所指定的
PostgreSQL 数据库的连接。如果成功则返回连接资源,如果不能连接则返回
FALSE
。connection_string
应该是用引号引起来的字符串。
Example #1 使用 pg_connect()
<?php
$dbconn = pg_connect("dbname=mary");
//connect to a database named "mary"
$dbconn2 = pg_connect("host=localhost port=5432 dbname=mary");
// connect to a database named "mary" on "localhost" at port "5432"
$dbconn3 = pg_connect("host=sheep port=5432 dbname=mary user=lamb password=foo");
//connect to a database named "mary" on the host "sheep" with a username and password
$conn_string = "host=sheep port=5432 dbname=test user=lamb password=bar";
$dbconn4 = pg_connect($conn_string);
//connect to a database named "test" on the host "sheep" with a username and password
?>
connection_string
所包括的参数有
host
,port
,tty
, options
,dbname
, user
和 password
。
如果用同样的 connection_string
再次调用
pg_connect(),不会建立新连接,而是返回前面已经打开的连接资源。如果使用不同的连接字符串,则可以和同一个数据库建立多个连接。
旧的多参数语法 $conn = pg_connect("host", "port", "options", "tty", "dbname") 已经不提倡使用。
参见 pg_pconnect(), pg_close(), pg_host(), pg_port(), pg_tty(), pg_options() 和 pg_dbname()。
Dave (2013-06-03 12:40:45)
If you use pgbouncer and unix socket
and you pgbouncer.ini looks like this
listen_port = 6432
unix_socket_dir = /tmp
you connect like this
pg_connect('host=/tmp port=6432 dbname=DB user=USER password=PASS');
dreamsoundaudio at gmail dot com (2011-09-03 10:06:24)
Ubuntu/Debian users, specifically server versions: If you used Tasksel to build PostgreSQL, and you're banging your head against the wall with the "Fatal error: Call to undefined function pg_connect()" error, check that php5-pgsql is installed.
Tasksel apparently doesn't install it.
pedro2009 at mandic dot com dot br (2011-06-26 11:55:01)
I had problems in a PHP program that uses fork() after pg_connect(); I didn't use any pg function inside the child, I just keep using pg functions in the parent process.
In this case, the connection gets "duplicated" and when the child finishes, it automatically calls pg_close() on its destructors, and this makes the parent connection to finish!
If your program needs to fork, then use this sequence:
pg_connect(); do_somethings(); pg_close(); fork(); pg_connect();
And remember: if your program forks, you CAN NOT use the pg_pconnect() function, because it is persistent.
thakur at corexprts dot com (2010-10-27 13:45:20)
One thing is to remember, whenever trying to use pg_connect, add the timeout parameter with it
<?php
$d=pg_connect('host=example.com user=pgsql dbname=postgres connect_timeout=5');
?>
infotirona at yahoo dot com (2010-08-16 18:33:37)
It's strange how this "Fatal error: Call to undefined function pg_connect()" happens(when everything else is OK) in PHP version 5.3.3.
I was trying to connect to my db when I got that error message the firs time. My extensions path was OK, pgsql extension should have been loaded from php.ini(i had enabled it before), Apache started-up without errors, but i still had the "Fatal error: Call to undefined function pg_connect()" message when i tried to connect.
Seaching a bit around i found something about dll libraries not working as they should, so deleted the new 5.3.3 version, downloaded the PHP 5.2.5 and configured it.
I'm using Windows XP Home SP3, Apache 2.2, PHP 5.2.5 and everything works fine now... ;)
kam (2010-07-09 07:17:32)
One more note about ssl connection to postgresql. I have a lot of problems with apache memory leaks (debian distro + apache2 + php5), and finally discovered that encrypted connection to postgresql causes those memory leaks.
With 'sslmode=disable' db connection is quickly and needs less memory. So, if you have not a special reason to use encrypted connection, I recommend to set it off.
Thank you, ratio, for your comment.
gutostraube at gmail dot com (2009-10-02 06:51:34)
It's possible connect to a PostgreSQL database via Unix socket using the pg_connect() function by the following two ways:
1) Using the socket path:
<?php
$conn = pg_connect('host=/var/run/postgresql user=username dbname=databasename');
?>
2) Omitting the host name/path:
<?php
$conn = pg_connect('user=username dbname=databasename');
?>
Note: in this case (omitting the host value), the default socket path will be used.
ratio (2009-08-28 03:57:34)
If the configuration parameter "ssl = true" is set in postgres.conf, and a pg_connect() is done without "sslmode=disable", then the connection seems to be established with SSL by default. Enabling SSL in Postgres can cause performance problems with large data transfers (e.g. bytea), because Postgres has a "built-in" native SSL Support, so Postgres has to do all the encryption.
bgalloway at citycarshare dot org (2008-03-27 18:33:14)
Beware about writing something like
<?php
function getdb_FAILS() {
return pg_connect("...") or die('connection failed');
}
?>
It will return a boolean. This will appear to be fine if you don't use the return value as a db connection handle, but will fail if you do.
Instead, use:
<?php
function getdb() {
$db = pg_connect("...") or die('connection failed');
return $db;
}
?>
which actually returns a handle.
tim at buttersideup dot com (2007-12-28 10:41:42)
It's not explicitly stated here, but you can also connect to PostgreSQL via a UNIX domain socket by leaving the host empty. This should have less overhead than using TCP e.g.:
$dbh = new PDO('pgsql:user=exampleuser dbname=exampledb password=examplepass');
In fact as the C library call PQconnectdb underlies this implementation, you can supply anything that this library call would take - the "pgsql:" prefix gets stripped off before PQconnectdb is called, and if you supply any of the optional arguments (e.g. user), then these arguments will be added to the string that you supplied... Check the docs for your relevant PostgreSQL client library: e.g.
http://www.postgresql.org/docs/8.3/static/libpq-connect.html
If you really want, you can use ';'s to separate your arguments - these will just be converted to spaces before PQconnectdb is called.
Tim.
xourge (2007-08-19 21:17:39)
remember that when you use a blank password there will be an error because of:
password= dbname= (...)
to fix this problem use '' in your $options variable
example:
$options = " host='localhost' port='5432' user='postgres' password='' dbname='test' ";
pg_connect($options);
*** careful: I used double ' after password=, not "
Sohel Taslim (2007-08-02 19:20:09)
I got the same problem but I have to solve that in different way.
In my postgresql.conf file the following was commented.
So, I active that under Connection Settings-
# - Connection Settings –
tcpip_socket = true
borovik -at- gmail (2007-04-03 07:06:09)
"If you use pg_connect('host=localhost port=5432 user=my_username password=my_password dbname=my_dbname') and you get the following error:
"Warning: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Connection refused Is the server running on host localhost and accepting TCP/IP connections on port 5432?"
"
I solved this error just by setting listen_addresses = '*' in the postgresql.conf file. This error occurs probably despite of a name resolution to localhost, given in the "host" parameter. So you can set the host in the pg_connect() function.
Anonymous (2005-04-10 09:51:37)
The values accepted by pg_connect's sslmode argument are: disable, allow, prefer, require
phpnet at benjamin dot schulz dot name (2004-09-01 04:28:00)
if you need to open a new connection handle (i.e. for multiple pg_send_query()) use PGSQL_CONNECT_FORCE_NEW as second parameter to pg_connect()
Cybertinus (2003-12-15 13:47:38)
If you use pg_connect('host=localhost port=5432 user=my_username password=my_password dbname=my_dbname') and you get the following error:
"Warning: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Connection refused Is the server running on host localhost and accepting TCP/IP connections on port 5432?"
then you should try to leave the host= and port= parts out of the connection string. This sounds strange, but this is an "option" of Postgre. If you have not activated the TCP/IP port in postgresql.conf then postgresql doesn't accept any incoming requests from an TCP/IP port. If you use host= in your connection string you are going to connect to Postgre via TCP/IP, so that's not going to work. If you leave the host= part out of your connection string you connect to Postgre via the Unix domain sockets, which is faster and more secure, but you can't connect with the database via any other PC as the localhost.
xzilla at users dot sourceforge dot net (2003-12-09 08:22:44)
regarding the note from matias at nospam dot projectcast dot com
on 12-Feb-2002 01:16, you do not need a user in the database with the same name a your web user with ANY version of postgresql. The only time that would be a requirement ifs if you set your postgresql server to only allow IDENT based authentication (which IIRC is the default on Red Hat systems, which might be what lead to the confusion). For more info on the various authentication methods allowed by postgresql, check out http://www.postgresql.org/docs/7.4/static/client-authentication.html
derry at siliconriver.com dot au (2003-08-07 20:48:28)
pg_connect seems to support SSL connections, on systems where Postgres has been compiled with ssl, i'm assuming this is since psql uses libpq to connect.
pg_connect can successfully connect, and use the "requiressl" argument.
jtate at php dot net (2002-12-31 14:36:29)
If you use host=HOSTNAME in your pg_connect string when connecting to PostgreSQL databases newer than 7.1, you need to make sure that your postmaster daemon is started with the "-i" option. Otherwise the connection will fail. See http://www.postgresql.org/idocs/index.php?client-authentication.html for client authentication documentation.
khyri at khyri dot com (2002-10-31 16:23:01)
After upgrading to PHP 4.2.3 from PHP 4.1.2 (Red Hat Linux Advanced Server with Stronghold 4.0) in order to manually compile in MHASH support, I discovered that Postgres support has disappeared, despite being specified on the command line, and compiling with no errors.
FATAL: Undefined function: pg_connect()
Confirmed by looking at the output of phpinfo() and comparing it to the output pre-upgrade - no mention of PostgreSQL in the new one.
Detective work led me to find that the old pgsql.so in /usr/lib/php4 was untouched, and the new one had ended up in /usr/lib/20020429 instead.
The fix was to edit config_vars.mk after configuration to change the value of EXTENSION_DIR, and then compile.
Not quite sure where 20020429 came from, looks like a left-over value from development testing...
Anyway, in case any one else has a similar problem, thought I'd document it here, as a problem with pg_connect is where this will first surface as a symptom.
Helio Ferenhof <d-m at eudoramail dot com> (2002-02-18 13:20:34)
Connection Hint:
Do you always write at the code the username and password to connect to your PostgreSQL database !?
What if your username or password changes?
Create a connection include file.
---
connection.inc
---
<?php
$connection = pg_connect("host=localhost port=5432 dbname=DATABASENAME user=USERNAME password=PASSWORD")
or die ("Nao consegui conectar ao PostGres --> " . pg_last_error($conn));
?>
// you can use $database name and pass it from the php file if you connect into different databases.
---
Phpfile.php
---
<?php
include('connection.php'); // Include the connection to the databank THEN start your SQL Job :)
$result=pg_exec("SELECT field FROM table WHERE field = '$something' "); // Sample of SQL QUERY
$fetch = pg_fetch_row($query_st); // Sample of SQL QUERY
pg_close($connection); // Close this connection
?>
[]?s
Helio Ferenhof
d-m@eudoramail.com
matias at nospam dot projectcast dot com (2002-02-12 13:16:58)
At least with Postgres 7.2, connecting to local postgresdatabase requires a user in the database with the same name as the user running apache, or the connection fails.
rolf at sir-wum dot de (2001-10-12 17:54:37)
pg_connect() won't work with the authentication method 'crypt' in the pg_hba.conf. Took me an hour to figure that out till I remeberd some other issues with windows missing the crypt() call.
kayotix at yahoo dot com (2000-09-14 23:54:39)
Little note that is buried in the install somewhere. In Php 3, PostgreSQL support was activated by adding --with-postgresql=[DIR] to the options passed to ./configure. With Php 4.0.2 (on Linux) the parameter was --with-pgsql. The only place I found this was in the installing PHP on Unix section of the manual.
leace at post dot cz (2000-07-21 14:26:17)
If you use PostgreSQL users for authenticating into your pg database rather than using your own authentication, always specify host directive in pg_connect and edit pg_hba.conf to authenticate from this host accordingly. Otherwise, PHP will connect as 'local' using UNIX domain sockets, which is set in pg_hba.conf to 'trust' by default (so you can connect using psql on console without specifying password) and everyone can connect to db _without password_ .