0

I have been using the official Strapi tutorial on how to deploy strapi to heroku with postgres and after following all the instructions, my heroku app is showing an error. However when I check the build logs, there are no errors and they show the build successful message.

build logs

2020-08-17T15:48:19.744258+00:00 app[web.1]: npm ERR! 
2020-08-17T15:48:19.744486+00:00 app[web.1]: npm ERR! Failed at the radio-darya-backend@0.1.0 start script.
2020-08-17T15:48:19.744753+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-08-17T15:48:19.756754+00:00 app[web.1]: 
2020-08-17T15:48:19.757071+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2020-08-17T15:48:19.757252+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2020-08-17T15_48_19_747Z-debug.log
2020-08-17T15:48:19.825573+00:00 heroku[web.1]: Process exited with status 1
2020-08-17T15:48:19.869487+00:00 heroku[web.1]: State changed from starting to crashed
2020-08-17T15:48:32.221633+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=radio-darya-backend.herokuapp.com request_id=1bceee5d-4452-4b2a-9638-d5f242b4337c fwd="213.162.246.193" dyno= connect= service= status=503 bytes= protocol=https
2020-08-17T15:48:32.751425+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=radio-darya-backend.herokuapp.com request_id=95d4de1a-5f17-49e3-bed2-b459bce9259e fwd="213.162.246.193" dyno= connect= service= status=503 bytes= protocol=https

package.json dependencies

  "devDependencies": {},
  "dependencies": {
    "knex": "<0.20.0",
    "pg": "^8.3.0",
    "sqlite3": "latest",
    "strapi": "3.1.4",
    "strapi-admin": "3.1.4",
    "strapi-connector-bookshelf": "3.1.4",
    "strapi-plugin-content-manager": "3.1.4",
    "strapi-plugin-content-type-builder": "3.1.4",
    "strapi-plugin-email": "3.1.4",
    "strapi-plugin-upload": "3.1.4",
    "strapi-plugin-users-permissions": "3.1.4",
    "strapi-utils": "3.1.4"
  },

config database.js

module.exports = ({ env }) => ({
  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'bookshelf',
      settings: {
        "client":"postgres",
        "host":"${process.env.DATABASE_HOST}",
        "port": "${process.env.DATABASE_PORT}",
        "database": "${process.env.DATABASE_NAME}",
        "username": "${process.env.DATABASE_USERNAME}",
        "password": "${process.env.DATABASE_PASSWORD}",
        "ssl": { "rejectUnauthorized": false }
      },
      options: {
       
      },
    },
  },
});
her

4 Answers4

1

This is working for me for v3.x strapi.

    // Path: ./config/env/production/database.js
    const parse = require('pg-connection-string').parse;
    const config = parse(process.env.DATABASE_URL);
    
    module.exports = ({ env }) => ({
      defaultConnection: 'default',
      connections: {
        default: {
          connector: 'bookshelf',
          settings: {
            client: 'postgres',
            host: config.host,
            port: config.port,
            database: config.database,
            username: config.user,
            password: config.password,
          },
          options: {
            ssl: false,
          },
        },
      },
    });

We also need to set the NODE_ENV variable on Heroku to production to ensure this new database configuration file is used.

heroku config:set NODE_ENV=production

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

Note here is v3.x instead of beta version. Google "strapi heroku postgres" for the moment still give legacy beta version.

anonymous
  • 1,372
  • 1
  • 17
  • 22
0

You didn’t give the error message you’re having?
I did deploy my strapi to heroku a few weeks ago and there was no problems.
You are sure you followed all the steps from strapi documentation?
Only thing I could think to go wrong is database connection.

First you have to install postgress addon in Heroku, then get the config-info and last add environment variables in Heroku (settings/config vars) and also modify strapi config-files to get the database information from environment variables.
Strapi documentation: https://strapi.io/documentation/3.0.0-beta.x/deployment/heroku.html.

EDIT:
Strapi documentation isn’t correct at the moment, database.json file and location has been changed. See:
strapi database.js / multiple database configs
https://www.youtube.com/watch?v=xNE0TrI5OKk

grohjy
  • 2,059
  • 1
  • 18
  • 19
  • I dont have an error message, because the build logs show that everything went fine. – Mysterious Shadow Aug 16 '20 at 20:38
  • It still didn't work after I updated my code, and theres still no error message in the build logs. – Mysterious Shadow Aug 16 '20 at 21:02
  • In your Heroku app configuration page there is a More-button in the top right corner. When you click it there is a “View logs”-link, that should give you more information what is happening when Heroku tries to fire up your application. If you still can’t get it working, I would recommend to make a fresh start in order to test the process. Install a new strapi -quickstart and verify dev working. Next create a new heroku app and postgres-addon, get info for env-variables and set them in heroku. Configure your strapi database config files, build and deploy to heroku. Test if everything works. – grohjy Aug 17 '20 at 05:13
  • I looked at the build logs and it says it fails at strapi start. I tried starting fresh but the same error happened again. – Mysterious Shadow Aug 17 '20 at 15:46
  • I believe it’s a database connection issue, everything else is pretty straight forward. You could leave environment variables out by writing database connection info to strapi config files. That will leave out some possible error source. Then you can write some wrong information to db config file (config/database.js) and see will you get different errors. If you are familiar with js, you could add some debug printing (debugging might need some other settings modified also) to database.js in order to see is the file read. – grohjy Aug 17 '20 at 16:23
  • I just noticed, if you install strapi without -quickstart option, installer will give you custom-option and then you can select database (postgres). After database selection installer will ask database connection configurations and there you can give heroku’s config info. You can use heroku’s postgres also in local dev strapi. This way you can leave out deploying to heroku and also manual edit of database.js-file. Strapi documentation for installer: https://strapi.io/documentation/3.0.0-beta.x/guides/databases.html#mongodb-installation – grohjy Aug 17 '20 at 16:31
0

ive just completed that tutorial a day ago... and i also had problems for a complete beginner to strapi and postgres and heroku... heres my experience.

follow along here to get postgres and the database setup: https://tute.io/install-configure-strapi-postgresql

then complete your setup with the missing pieces from here: https://strapi.io/documentation/v3.x/deployment/heroku.html

basically: have postgres installed locally on your system, create the db and user and grant permissions. install strapi without the quickstart flag and use the details that was used above. use the heroku cli to set the configs derived from the database_url configvar. commit and push and all should be well.

EDIT: In Heroku under the app in question:

  • click setting and scroll down to Environment variable and click Reveal Config Vars
  • Ensure that you have a DATABASE_URL config var
  • Ensure that your postgres specific config vars match up to the DATABASE_URL

eg. DATABASE_URL = postgres://ebitxebvixeeqd:dc59b16dedb3a1eef84d4999sb4baf@ec2-50-37-231-192.compute-2.amazonaws.com: 5432/d516fp1u21ph7b

  • It's read like so: postgres:// USERNAME : PASSWORD @ HOST : PORT : DATABASE_NAME

Also in your ./config/server.js file make sure your host is 0.0.0.0

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

Also change your database.js config to be:

settings: {
    client: 'postgres',
    host: env('DATABASE_HOST', '127.0.0.1'),
    port: env.int('DATABASE_PORT', 5432),
    database: env('DATABASE_NAME', 'strapi'),
    username: env('DATABASE_USERNAME', 'postgres'),
    password: env('DATABASE_PASSWORD', ''),
    ssl: env.bool('DATABASE_SSL', false),
  },
  options: {}

Theres no much to go on from your question or the logs, but the above is basically some common issues ive experienced.

Muhammad Ahmod
  • 649
  • 4
  • 15
-1

copy this code directly in the config/database.js

module.exports = ({ env }) => ({
defaultConnection: 'default',
connections: {
default: {
connector: 'bookshelf',
settings: {
client:'postgres',
host:`${process.env.DATABASE_HOST}`,
port: `${process.env.DATABASE_PORT}`,
database: `${process.env.DATABASE_NAME}`,
username: `${process.env.DATABASE_USERNAME}`,
password: `${process.env.DATABASE_PASSWORD}`, 
ssl: { "rejectUnauthorized": false }
},
options: {
},
},
},
});  
vinay sandesh
  • 918
  • 9
  • 8
  • I'm having this problem too, but that doesn't work. I didn't have the environments/production directories in my config folder, so created them. I kept my database.js file the same like you said, but it doesn't work. This isn't the solution I don't think. – Jon Nicholson Nov 15 '20 at 13:44
  • yep my bad ! please do follow this step it is working and I have tested it ! – vinay sandesh Dec 14 '20 at 07:18
  • and also you will have to configure your heroku with your postgress env variables from the heroku cli for DATABSAE_HOST,DATABASE_PORT,DATABASE_NAME, DATABASE_USERNAME,DATABASE _PASSWORD – vinay sandesh Dec 14 '20 at 07:29