Platform.sh User Documentation

Configure PostgreSQL for Strapi on Platform.sh

Sign up for Upsun

Get your free trial by clicking the link below.

Get your Upsun free trial

Strapi can be configured to use PostgreSQL as its default database. You can choose PostgreSQL when installing your app by selecting custom and PostgreSQL when asked for the installation type or you can just configure your existing Strapi application to use PostgreSQL. To configure a PostgreSQL database for Strapi on Platform.sh, follow these steps.

  1. Install PostgreSQL on your machine and pg in your Strapi project. Pg comes installed with a fresh Strapi installation.

  2. In your .platform/services.yaml file, add the following:

    	postgres:
    	     type: postgresql:13
             disk: 512
  3. In your .platform.app.yaml file, replace the relationship name to match the PostgreSQL database you added:

    relationships:
       postgresdatabase: "postgres:postgresql"
  4. In the config folder, locate the database.js file, and replace its content with the following:

    const path = require("path");
    const config = require("platformsh-config").config();
    
    let dbRelationship = "postgresdatabase";
    
    // Strapi default sqlite settings.
    let connection = {
      connection: {
        client: "sqlite",
        connection: {
          filename: path.join(
            __dirname,
            "..",
            process.env.DATABASE_FILENAME || ".tmp/data.db"
          ),
        },
        useNullAsDefault: true,
      },
    };
    
    if (config.isValidPlatform() && !config.inBuild()) {
      // Platform.sh database configuration.
      const credentials = config.credentials(dbRelationship);
      console.log(
        `Using Platform.sh configuration with relationship ${dbRelationship}.`
      );
    
      let pool = {
        min: 0,
        max: 10,
        acquireTimeoutMillis: 600000,
        createTimeoutMillis: 30000,
        idleTimeoutMillis: 20000,
        reapIntervalMillis: 20000,
        createRetryIntervalMillis: 200,
      };
    
      connection = {
        connection: {
          client: "postgres",
          connection: {
            host: credentials.ip,
            port: credentials.port,
            database: credentials.path,
            user: credentials.username,
            password: credentials.password,
            ssl: false,
          },
          debug: false,
          pool,
        },
      };
    } else {
      if (config.isValidPlatform()) {
        // Build hook configuration message.
        console.log(
          "Using default configuration during Platform.sh build hook until relationships are available."
        );
      } else {
        // Strapi default local configuration.
        console.log(
          "Not in a Platform.sh Environment. Using default local sqlite configuration."
        );
      }
    }
    
    console.log(connection);
    
    // export strapi database connection
    module.exports = ({ env }) => connection;

This setting deploys your Strapi application with a PostgreSQL database. If you don’t specify a PostgreSQL service in your .platform/services.yaml file or the Strapi application is run on a local machine the default SQLite database is used.

Is this page helpful?