Set appName for PDO connections made via PHP

Viewed 104

I use PDO in PHP to connect to a PostgreSQL database. Does it allow setting the application name when making this connection?

try {
    $conn = new PDO ( 'pgsql:host=' . $host . ';dbname=' . $dbname, $user, $pwd );
    //$conn->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch ( PDOException $e ) {
    showError ( "Error", 2 );
}
1 Answers

You can add to the DSN string a new element called options with value --application_name=YOUR_NAME_HERE:

$conn = new PDO('pgsql:host=' . $host . ';dbname=' . $dbname . ';options=--application_name=YOUR_NAME_HERE', $user, $pwd );

enter image description here

Credit goes for this old user comment in PHP manual.

Related