Connect with PHP Early availability
Connect to an Aiven for AlloyDB Omni database from PHP, using the built-in PDO module.
Prerequisites
- Aiven for AlloyDB Omni service running
- CA certificate (a file named
ca.pem
) downloaded from the service's Overview page - PostgreSQL functions included in your PHP installation (most installations include them)
Connect to a service
-
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.
noteTo verify the SSL certificate, this code specifies
sslmode
asverify-ca
and adds the location of the certificate. -
Run the code:
php index.php
Expect output like:
PostgreSQL 15.5 on x86_64-pc-linux-gnu, compiled by [...]