Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set appName for PDO connections made via PHP

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 );
}
like image 327
Tom Avatar asked Jan 25 '26 14:01

Tom


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
);

PostgreSQL client showing application_name among connection data

Credit goes for this old user comment in PHP manual.

like image 67
Álvaro González Avatar answered Jan 28 '26 04:01

Álvaro González



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!