Runway
Runway is built and operated in the EU!

PHP Extensions

The best way to enable additional PHP extensions on Runway is composer.

In this guide, you will learn how to enable the PDO and pdo_pgsql extensions on Runway, to be able to connect to your Runway (PostgreSQL) database from your application.

Available extensions

The following extensions are available but not enabled by default. For brevity, we’ll use PDO and pdo_pgsql in our example, but any other extension in the list will work.

APIs

Caching

Compression

Database

Debugging

Email

Graphics

Processes

Queues

Security

String handling

Misc

Require extensions

Create a file called composer.json in your repository and add the following:

composer.json
{
  "require": {
    "php": "8.1.*",
    "ext-pdo": "*",
    "ext-pdo_pgsql": "*"
  }
}

If you’re using composer already, then add ext-pdo and ext-pdo_psql to the require section of the composer.json file so it looks similar to the above.

You must ensure the composer.lock is updated and commit both composer files to Git:

composer update --ignore-platform-reqs
git add composer.lock
git add composer.json
git commit -m 'set php extensions'

Alternative

The alternative to composer is to create a directory called .php.ini.d in your application, and add a file called extension.ini into it. The content of the file is the following:

.php.ini.d/extension.ini
extension=pdo.so
extension=pdo_pgsql.so

Example

Once the extensions PDO and pdo_pgsql are added, here is an example how to use them to connect to your database on Runway.

Add the DATABASE_URL to your application configuration:

runway app config set $(runway service dsn db-php-app)

Add the following code to your index.php:

htdocs/index.php
<?php
// parse URL to values that PDO understands
$dbConfig = parse_url(getenv('DATABASE_URL'));
$dbConnection = sprintf(
  'pgsql:host=%s;dbname=%s',
  $dbConfig['host'],
  ltrim($dbConfig['path'], '/')
);

// connect
try {
  $pdo = new PDO($dbConnection, $dbConfig['user'], $dbConfig['pass']);
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $stmt = $pdo->query("SELECT 1");
  echo "<p>Connected 🤘</p>";
} catch (Exception $e) {
  die("DB Error: " . $e->getMessage());
}

git commit the updated index.php file before you proceed to deploy your PHP application: runway app deploy