PHP Velho Oeste 2024

PDO_PGSQL DSN

(PHP 5 >= 5.1.0, PHP 7, PECL PDO_PGSQL >= 0.1.0)

PDO_PGSQL DSNPostgreSQL データベースに接続する

説明

PDO_PGSQL データソース名 (DSN) は以下の要素で構成され、 スペースあるいはセミコロンで区切られます:

DSN 接頭辞

DSN 接頭辞は pgsql: です。

host

データベースサーバーが存在するホスト名を指定します。

port

データベースサーバーが起動しているポートを指定します。

dbname

データベース名を指定します。

user

接続するユーザー名を指定します。もし DSN でユーザー名を指定する場合、 PDO は PDO コンストラクタにおけるユーザー名引数の値を無視します。

password

接続するユーザーのパスワードを指定します。もし DSN でパスワードを指定する場合、 PDO は PDO コンストラクタにおけるパスワード引数の値を無視します。

sslmode

SSLモード。サポートされている値とその意味については、 » PostgreSQL Documentation を参照ください。

注意: DSN 文字列における全てのセミコロンは、スペースで置き換えられます。 なぜなら、PostgreSQL はそのように置き換えられたフォーマットを期待しているからです。 このことは、DSN におけるあらゆるコンポーネント (例: passworddbname) で、セミコロンはサポートされていないということです。

例1 PDO_PGSQL DSN の例

以下の例は、PostgreSQL データベースに接続するための PDO_PGSQL DSN を示します:

pgsql:host=localhost;port=5432;dbname=testdb;user=bruce;password=mypass

以下の例は、unix ソケット経由で PostgreSQL データベースに接続するための PDO_PGSQL DSN を示します:

pgsql:host=/tmp;port=5432;dbname=testdb;user=bruce;password=mypass

add a note add a note

User Contributed Notes 7 notes

up
20
tim at buttersideup dot com
16 years ago
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.
up
7
enclaved
8 years ago
I must say Chris C. is correct, yet sadly downvoted. It is irrelevant if semicolons are converted to spaces in the DSN string. The entire point of PDO is expressed in the first word of the acronym: "Portable"; it provides a uniform way of accessing databases of various flavors, and since a semicolon is its standard DSN delimiter, it SHOULD be used (at miniscule overhead if taking conversion into account, mind you) to facilitate uniformity and potentially ease portability.
up
7
other at yetterprises dot com
10 years ago
I wanted to point out something that is not obvious from the documentation here or in any google searches that I've done.  Everything after the 'pgsql:' is passed to the PQconnectdb function of the pgsql library as a connection string.  This means 2 important key things:

1) 'username' is not a valid option of the PQconnectdb connection string.  Use 'user' instead (ie 'user=<username>' instead of 'username=<username>'). 
2) You can utilize ANY option of the PQconnectdb function in this way.  For those trying to figure out how to enable ssl connections through the pgsql PDO connection process, like me, as per Postgresql standards, the 'sslmode' parameter can be set via the DSN.  So, for example, 'sslmode=require' will require an ssl connection.  Look at the documentation for the pgsql library version you are using for all options of both the sslmode parameter and any other parameters that are available.
up
15
Chris C.
18 years ago
The PDO_PGSQL DSN should be seperated by semi-colons not spaces. It should follow the convention like the rest of the PDO DSNs.

'pgsql:dbname=example;user=nobody;password=change_me;host=localhost'
up
7
fandi
8 years ago
We can also alternatively put the username and password in the second and third argument of PDO::__construct()
<?php
$dbh
= new PDO("pgsql:dbname=$dbname;host=$host", $dbuser, $dbpass);
?>
up
-1
Ivan Bragin (aywan at aywan dot ru)
6 years ago
How to add application name to dsn string.
You can make query like "SET application_name=...." or just add something to dsn string:
$dbh = new PDO('....;options=--application_name=WEB_APPLICATION');

We have a lot of php apps and it's hard to understand what app made slow query. But this small hack help us.
up
-77
tmairs at aasland dot com
14 years ago
The DSN syntax shown here did not work for me, but this did:

<?php
$dbh
= new PDO("pgsql:dbname=$dbname;host=$host", $username, $password );
?>

As opposed to

<?php
$dbh
= new PDO('pgsql:dbname=$dbname;
                           host=$host;
                           username=$username;
                           password=$password'
);
?>

Which makes sense and is more PGSQL standard.
To Top