Skip to main content

Connect with PHP Early availability

Connect to an Aiven for AlloyDB Omni database from PHP, using the built-in PDO module.

Prerequisites

Connect to a service

  1. Create a file named index.php with the following content:

    <?php

    $uri = "POSTGRESQL_URI";

    $fields = parse_url($uri);

    // build the DSN including SSL settings
    $conn = "pgsql:";
    $conn .= "host=" . $fields["host"];
    $conn .= ";port=" . $fields["port"];;
    $conn .= ";dbname=defaultdb";
    $conn .= ";sslmode=verify-ca;sslrootcert=ca.pem";

    $db = new PDO($conn, $fields["user"], $fields["pass"]);

    foreach ($db->query("SELECT VERSION()") as $row) {
    print($row[0]);
    }

    Replace SERVICE_URI with the service URI available on the Overview page in the Aiven Console.

    This code opens a connection to the database, runs a query checking the database version, and prints the response.

    note

    To verify the SSL certificate, this code specifies sslmode as verify-ca and adds the location of the certificate.

  2. Run the code:

    php index.php

Expect output like:

PostgreSQL 15.5 on x86_64-pc-linux-gnu, compiled by [...]