Getting strapi working on digitalocean app plattform

Hi, just wondering if someone got strapi v4 working on digitalocean app plattform with postgres.

Here is what I did so far.

  1. created a postgres 13 db and added ari-motors-v2 as trusted service
  2. setup app plattform as a node js service from github.
  3. Add db as component to the app plattform
  4. Set env var DATABASE_URL: ${defaultdb.DATABASE_URL}
  5. Read config in database.js (this worked on heroku already)
    const { host, port, database, user, password } = parse(
      process.env.DATABASE_URL
    );

    return {
      connection: {
        client: "postgres",
        connection: {
          host,
          port,
          database,
          user,
          password,
        },
      },
    };

Build works, but db connection fails in the end.

Thats the db string
postgresql://doadmin:password@db-postgresql-fra1-48455-do-user-9714386-0.b.db.ondigitalocean.com:25060/defaultdb?sslmode=require

Does the part sslmode=require mean the server needs an ssl cert to work?
I tried to set ssl: false as descibed as a workaround in the docs.

System Information
  • Strapi Version: 4.0.0
  • Operating System: fedora
  • Database: postgres 13
  • Node Version: 16
  • NPM Version: 8

1 Like

Here are the changes to get it working:

  1. Delete the component database in app plattform and only use the previously created db
  2. Hard code the connection string to app plattform env (picture below)
  3. Set NODE_ENV to production (picture below)
    image
  4. Set ssl in database.js like this.

ssl: { rejectUnauthorized: env.bool(“DATABASE_SSL_SELF”, false), // For self-signed certificates }

Hello,

Does your solution really work? I followed your instructions and nothing :frowning:
Is impossible to connect to the production DO database.

1 Like

Hi @rmartis and everyone else reading this,

I had a hard time as well figuring out how to connect the APP and Database on Digitalocean. Here’s what worked for me:

As @tw1t611 mentioned, the DATABASE_URL: ${<<yourDbName>>.DATABASE_URL} env variable doesn’t seem to work as we expect it at the moment. So setting the variable to the connection string is fixing this.
BUT: I figured out that DO sets the DATABASE_URL env automatically when you connect a database to the app.
In my case: These env variable is not shown in the Environment Variables section in the settings but in the config .yaml file (under a second envs key). In my case there where 3 DATABASE_URL variables because I changed connected DBs. I had to delete all of them to make my manually set DATABASE_URL (the connection string) env working again. Alternatively you can just name your variable differently.

In my code:
I had to install pg and pg-connection-string packages and add these code to my config/production/database.js:

const parse = require("pg-connection-string").parse;

const { host, port, database, user, password } = parse(
  process.env.DATABASE_URL
);
module.exports = () => ({
  connection: {
    client: "postgres",
    connection: {
      host,
      port,
      database,
      user,
      password,
      ssl: {
        rejectUnauthorized: false,
      },
    },
    debug: false,
  },
});


I’m not sure if these is the best way to do it but it worked out for me. Reach out if I should further clarify.

1 Like

Lifesaver!

Posted it at:

1 Like

Just want to thank you. I’ve been for 6 or 8 straight hours without any hope of fixing the issue. Strapi couldnt pass the proper values on the production env, and after i made these suggested changes it finaly built

1 Like