1

Trying to deploy Strapi on Heroku with Postgres as described here

https://strapi.io/documentation/v3.x/deployment/heroku.html

But I get this error

error: no pg_hba.conf entry for host "84.212.51.43", user "ssqqeaz***", database "d6gtu***", SSL off

I use Heroku Postgres add-on.

My database config:

module.exports = ({ env }) => ({
  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'bookshelf',
      settings: {
        client: 'postgres',
        host: env('DATABASE_HOST', '127.0.0.1'),
        port: env.int('DATABASE_PORT', 27017),
        database: env('DATABASE_NAME', 'strapi'),
        username: env('DATABASE_USERNAME', ''),
        password: env('DATABASE_PASSWORD', ''),
      },
      options: {
        ssl: true
      },
    },
  },
});

Why? Please help!

podeig
  • 2,597
  • 8
  • 36
  • 60
  • 1
    Does this answer your question? [Heroku Database Connection Properties](https://stackoverflow.com/questions/17377118/heroku-database-connection-properties) – RangerRanger May 29 '20 at 02:47
  • How do add ?ssl=true to the connection string. I try to change DATABASE_URL on Heroku UI but I get a message: Item cannot be updated. How can I update it? – podeig May 29 '20 at 13:20

3 Answers3

1

try to change ssl : true into ssl : false

wongdarjo
  • 19
  • 2
0

The current configuration you've posted will not work with a Heroku Postgres database. The primary concern here is that you're reading components of your postgres database url out of manually set config vars. This is very much recommended against by Heroku because they may need to move the database to a new host in the case of disasters. DATABASE_URL is set by Heroku when you create a database on an app and it's the one config var you can rely on to stay up-to-date. Moving on...

You will need to parse the username, password, host, port and database name out of the DATABASE_URL config var and supply those to the attributes of the settings block. Based on the error you provided, I can tell you're not presently doing this because Heroku databse usernames all start with a 'u', so something is very wrong if you get the error user "ssqqeaz***". As a first step you might try hard coding these values in the settings block to make sure it works (make sure to rotate the credentials after you do it, or otherwise clean up your git history to prevent leaked creds). The pattern for a postgres connection url is something like this: postgres:// $USERNAME : $PASSWORD @ $HOSTNAME : $PORT / $DATABASE_NAME.

RangerRanger
  • 2,455
  • 2
  • 16
  • 36
  • Hi, Thank you for the answer. I do not understand how I have to extract Username/Password etc... from DATABASE_URL. Should not strapi do it for me? The second thing the site works locally so username which starts from "ssqq***" is totally correct. :-/ – podeig Jun 02 '20 at 08:11
  • Should Strapi do that for you? Yes, it probably should but it looks like it doesn't. Many ORMs have first class support for `DATABASE_URL` and are able to dynamically parse out the components (username, pword, host, etc) and apply them to the configuration. Unfortunately, you'll need to do that manually it looks like. A quick Google search shows npm packages that help with this, maybe you can look at those for inspiration. Regarding the username, are you using Postgres Credentials with Herkoku Postgres? Because all default credentials begin with a "u". – RangerRanger Jun 02 '20 at 12:16
0

Not sure if it will help moving your config around...

  • remove ssl from option Key

  • insert ssl after password inside of settings Key eg.

    ssl: env.bool('DATABASE_SSL', false),

also check your app config vars inside of Heroku and make sure you have the required postgres config vars setup and they match the heroku generated DATABASE_URL config var. lastly check your ./config/server.js file and make sure your host is 0.0.0.0

eg.

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
  admin: {
    auth: {
      secret: env('ADMIN_JWT_SECRET', '**********************************'),
    },
  },
});
Muhammad Ahmod
  • 649
  • 4
  • 15