Create a TCP connection by using PHP

Open a TCP connection to Cloud SQL PostgreSQL by using the PHP Data Objects (PDO) extension.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

PHP

// $username = 'your_db_user';
// $password = 'yoursupersecretpassword';
// $dbName = 'your_db_name';
// $dbHost = "127.0.0.1";

// Connect using TCP
$dsn = sprintf('pgsql:dbname=%s;host=%s', $dbName, $dbHost);

// Connect to the database
$conn = new PDO($dsn, $username, $password, $connConfig);

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.