suche nach in der

pg_connection_busy> <pg_close
Last updated: Fri, 18 May 2012

view this page in

pg_connect

(PHP 4, PHP 5)

pg_connectÖffnet eine PostgreSQL-Verbindung

Beschreibung

resource pg_connect ( string $connection_string [, int $connect_type ] )

pg_connect() öffnet eine Verbindung zu der PostgreSQL-Datenbank, die durch den connection_string bezeichnet wird.

Falls ein zweiter Aufruf von pg_connect() mit demselben connection_string gemacht wird, wird die Verbindungskennung der bereits geöffneten Verbindung zurückgegeben, es sei denn, Sie übergeben die Konstante PGSQL_CONNECT_FORCE_NEW im Parameter connect_type.

Die alte Syntax $conn = pg_connect ("host", "port", "options", "tty", "dbname") sollte nicht mehr benutzt werden.

Parameter-Liste

connection_string

Der connection_string darf leer sein, dann werden Standard-Parameter benutzt. Er kann auch einen oder mehrere Parameter, durch Leerzeichen getrennt, enthalten. Jeder Parameter muss in der Form keyword = value angegeben werden, wobei das Gleichheitzeichen optional ist. Um einen leeren Wert oder einen Wert, der Leerzeichen enthält, zu übergeben, muss dieser in einfache Anführungszeichen eingeschlossen sein, etwa so: keyword = 'ein Wert'. Einfache Anführungszeichen oder Backslashes innerhalb von Werten müssen mit einem Backslash maskiert werden: \' und \\.

Diese Schlüsselwörter für die Parameter werden aktuell erkannt: host, hostaddr, port, dbname, user, password, connect_timeout, options, tty (wird ignoriert), sslmode, requiressl (zugunsten von sslmode ausgemustert) und service. Welche dieser Parameter zur Verfügung stehen, ist von Ihrer PostgreSQL-Version abhängig.

connect_type

Wenn PGSQL_CONNECT_FORCE_NEW übergeben wird, wird eine neue Verbindung aufgebaut, auch wenn der connection_string identisch zu der aktuell geöffneten Verbindung ist.

Rückgabewerte

Bei Erfolg wird eine PostgreSQL-Verbindungskennung zurückgegeben oder FALSE bei einem Fehler.

Beispiele

Beispiel #1 pg_connect() benutzen

<?php
$dbconn 
pg_connect("dbname=mary");
//Verbindungsaufbau zu einer Datenbank namens "mary"

$dbconn2 pg_connect("host=localhost port=5432 dbname=mary");
// Verbindungsaufbau zu einer Datenbank namens "mary" auf dem
// Host "localhost" Ã¼ber den (Standard-)Port "5432"

$dbconn3 pg_connect("host=sheep port=5432 dbname=mary user=lamb password=foo");
//Verbindungsaufbau zu einer Datenbank namens "mary" auf dem Host
//"sheep" mit dem Benutzernamen "lamb" und dem Passwort "foo"

$conn_string "host=sheep port=5432 dbname=test user=lamb password=bar";
$dbconn4 pg_connect($conn_string);
//Verbindungsaufbau zu einer Datenbank namens "test" auf dem Host
//"sheep" mit dem Benutzernamen "lamb" und dem Passwort "bar"
?>

Siehe auch

  • pg_pconnect() - Öffnet eine persistente PostgreSQL-Verbindung
  • pg_close() - Schließt eine PostgreSQL-Verbindung
  • pg_host() - Gibt den Namen des Host zurück, zu dem verbunden wurde
  • pg_port() - Gibt die Portnummer zurück, über die die Verbindung aufgebaut wurde
  • pg_tty() - Gibt den TTY Namen für die Verbindung zurück
  • pg_options() - Gibt die Verbindungsoptionen der aktuellen Verbindung zurück
  • pg_dbname() - Gibt den Namen der Datenbank zurück



add a note add a note User Contributed Notes
pg_connect
borovik -at- gmail
03-Apr-2007 04:06
"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
10-Apr-2005 06:51
The values accepted by pg_connect's sslmode argument are: disable, allow, prefer, require
phpnet at benjamin dot schulz dot name
01-Sep-2004 01:28
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
15-Dec-2003 10:47
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
09-Dec-2003 05:22
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
08-Aug-2003 05:48
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
31-Dec-2002 11:36
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
01-Nov-2002 01:23
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>
18-Feb-2002 10:20
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
12-Feb-2002 10:16
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
13-Oct-2001 02:54
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
15-Sep-2000 08:54
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
21-Jul-2000 11:26
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_ .

pg_connection_busy> <pg_close
Last updated: Fri, 18 May 2012