Goal:
Access credentials for PostgreSQL from within a Platform.sh application using PHP.
Assumptions:
- an active Platform.sh project
- properly configured
.platform/services.yaml
for the given service - SSH key configured on account if developing locally
- PostgreSQL installed if developing locally
- properly configured PostgreSQL relationship like so, in
.platform.app.yaml
:
relationships:
database: "dbpostgres:postgresql"
If developing locally, remember to first open a tunnel to the project environment using the Platform CLI.
$ platform tunnel:open && export PLATFORM_RELATIONSHIPS="$(platform tunnel:info --encode)"
This will open an SSH tunnel to your current Platform.sh environment and expose a local environment variable that mimics the relationships array on Platform.sh. Check the Platform.sh documentation and How to develop locally on Platform.sh with a tethered connection for more information.
Problems
Platform.sh service credentials are made available to applications as the PLATFORM_RELATIONSHIPS
environment variable, which is a base64-encoded JSON string that has to be decoded before it can be used.
There are two primary options for accessing service credentials on Platform.sh that can be used within an application:
- Using the Platform.sh Config Reader library
- Accessing environment variables directly
Steps (Config Reader)
1. Install the library
Install the configuration library. See the documentation for minimum requirements.
$ composer install platformsh/config-reader
2. Create a Config object
Creating a Config
object provides access to the Platform.sh environment.
use Platformsh\ConfigReader\Config;
$config = new Config();
3. Read the credentials
// Get the credentials to connect to the PostgreSQL service.
$credentials = $config->credentials('database');
Steps (Manual)
1. Load and decode the environment variable
$relationships = json_decode(base64_decode(getenv('PLATFORM_RELATIONSHIPS')), TRUE);
2. Read the credentials
$credentials = $relationships['database'];
Use:
path
is the database name that will be needed to connect. Pass the needed properties to your PostgreSQL library’s setup routine in your application. Most of the time the other values may be ignored.
In either case, credentials
is now an object matching the relationship JSON object.
{
"username": "main",
"scheme": "pgsql",
"service": "dbpostgres",
"ip": "169.254.231.161",
"hostname": "tkydopuhmksuhglz7ox4x4de6m.postgresql.service._.eu-3.platformsh.site",
"cluster": "rjify4yjcwxaa-master-7rqtwti",
"host": "database.internal",
"rel": "postgresql",
"path": "main",
"query": {
"is_master": true
},
"password": "main",
"type": "postgresql:11",
"port": 5432
}
Conclusions:
Using either the language-specific Platform.sh Configuration library or direct access methods for environment variables, an application can get PostgreSQL credentials in PHP.
Platform.sh supports configuration libraries for multiple languages. The PHP configuration library can be useful for connecting to PostgreSQL without creating the full DSN string using FormattedCredentials
:
// Define formatted credentials
$formatted = $config->FormattedCredentials("database", "pdo_pgsql")
The APIs for each language are written to be as consistent as possible, but seek out each library’s documentation for specific differences.