Error: connect ECONNREFUSED error when starting the app using command NODE_ENV=production npm run start

System Information
  • Strapi Version: 4.6.0
  • Operating System: Windows 10 Home
  • Database:
  • Node Version: 16.19.1
  • NPM Version: 8.19.3
  • Yarn Version:

I am on the 2nd bullet point on the ‘prerequisites’ section of the Azure Deployment documentation.

I am following the Application Configuration section of the Deployment documentation.

My server.js file looks like this:

module.exports = ({ env }) => ({
  host: env('APP_HOST', '0.0.0.0'),
  port: env.int('NODE_PORT', 1337),
  app: {
    keys: env.array('APP_KEYS'),
  },
});

My .env file looks like this:

HOST=0.0.0.0
PORT=1337
APP_KEYS=oToAkaDoii4O10uurIF79g==,ufULvhxXWLsohCGkH8W/Ew==,2p+sd4Dd1GgfPVR/RepeFg==,fiej/5ySVc70jHuGMospsA==
API_TOKEN_SALT=dyLTjQaSrrA0s/F6p9JDeg==
ADMIN_JWT_SECRET=CUYnLYYv348RHQV0yWGATw==
JWT_SECRET=ZOiNU4+C3Vb1BZ8dRTjg+A==
APP_HOST=0.0.0.0
NODE_PORT=1337

I have successfully been able to build the Admin panel for production without errors, using the following command:

NODE_ENV=production npm run build

However, when running the command to run the server with the production settings, using the following command:

NODE_ENV=production npm run start

I get the following errors in the terminal:

$ NODE_ENV=production npm run start

> backend@0.1.0 start
> strapi start       

[2023-02-28 15:28:07.817] debug: ⛔️ Server wasn't able to start properly.
[2023-02-28 15:28:07.822] error: connect ECONNREFUSED 127.0.0.1:5432 
Error: connect ECONNREFUSED 127.0.0.1:5432
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1278:16)

It’s trying to connect to postgres and it’s not responding that it’s not existing or running.

I don’t believe to have set the app to use postgres.

In the config/database.js file, I have set the client connection to be with sqlite.

const path = require('path');

module.exports = ({ env }) => {
  const client = env('DATABASE_CLIENT', 'sqlite');

  const connections = {
    mysql: {
      connection: {
        connectionString: env('DATABASE_URL'),
        host: env('DATABASE_HOST', 'localhost'),
        port: env.int('DATABASE_PORT', 3306),
        database: env('DATABASE_NAME', 'strapi'),
        user: env('DATABASE_USERNAME', 'strapi'),
        password: env('DATABASE_PASSWORD', 'strapi'),
        ssl: env.bool('DATABASE_SSL', false) && {
          key: env('DATABASE_SSL_KEY', undefined),
          cert: env('DATABASE_SSL_CERT', undefined),
          ca: env('DATABASE_SSL_CA', undefined),
          capath: env('DATABASE_SSL_CAPATH', undefined),
          cipher: env('DATABASE_SSL_CIPHER', undefined),
          rejectUnauthorized: env.bool(
            'DATABASE_SSL_REJECT_UNAUTHORIZED',
            true
          ),
        },
      },
      pool: { min: env.int('DATABASE_POOL_MIN', 2), max: env.int('DATABASE_POOL_MAX', 10) },
    },
    postgres: {
      connection: {
        connectionString: env('DATABASE_URL'),
        host: env('DATABASE_HOST', 'localhost'),
        port: env.int('DATABASE_PORT', 3306),
        database: env('DATABASE_NAME', 'strapi'),
        user: env('DATABASE_USERNAME', 'strapi'),
        password: env('DATABASE_PASSWORD', 'strapi'),
        ssl: env.bool('DATABASE_SSL', false) && {
          key: env('DATABASE_SSL_KEY', undefined),
          cert: env('DATABASE_SSL_CERT', undefined),
          ca: env('DATABASE_SSL_CA', undefined),
          capath: env('DATABASE_SSL_CAPATH', undefined),
          cipher: env('DATABASE_SSL_CIPHER', undefined),
          rejectUnauthorized: env.bool(
            'DATABASE_SSL_REJECT_UNAUTHORIZED',
            true
          ),
        },
        schema: env('DATABASE_SCHEMA', 'public'),
      },
      pool: { min: env.int('DATABASE_POOL_MIN', 2), max: env.int('DATABASE_POOL_MAX', 10) },
    },
    sqlite: {
      connection: {
        filename: path.join(
          __dirname,
          '..',
          env('DATABASE_FILENAME', 'data.db')
        ),
      },
      useNullAsDefault: true,
    },
  };

  return {
    connection: {
      client,
      ...connections[client],
      acquireConnectionTimeout: env.int('DATABASE_CONNECTION_TIMEOUT', 60000),
    },
  };
};

I noticed in my project solution file explorer a new folder in the config folder called env and it contains a subfolder production with a file database.js:
[project name]\config\env\production\database.js

The file content looks like the below:

    // path: ./config/env/production/database.js

    module.exports = ({ env }) => ({
        connection: {
          client: 'postgres',
          connection: {
            host: env('DATABASE_HOST'),
            port: env.int('DATABASE_PORT'),
            database: env('DATABASE_NAME'),
            user: env('DATABASE_USERNAME'),
            password: env('DATABASE_PASSWORD'),
            ssl: {
              ca: env('DATABASE_CA')
            },
          },
          debug: false,
        },
      });

I believe this was created when following the Application Configuration deployment steps, as I executed the following command to build the project for production:

NODE_ENV=production npm run build

I’m not sure if the subfolder production with file database.js was created immendiately after executing the preceding build command for production, because immediately after, I executed the following command to run the server with the production settings (to which it failed resulting in the error outlined in the original post):

NODE_ENV=production npm run start

Either way, I think that preparing the application for production using the preceding execution commands, by default, creates a subfolder production with a file database.js in the path ./config/env/production/database.js, thereby setting postgres as the default production database.

I had to download Postgres which installed pgAdmin alongside it.

Then I had to make sure that the connection is using the right environment variables from the .env file:
[project_folder]\backend.env
The following is the content of this file after I got it working:

HOST=0.0.0.0
PORT=1337
APP_KEYS=[my-key]
API_TOKEN_SALT=[my-api-token-salt]
ADMIN_JWT_SECRET=[my-admin-jwt-secret]
JWT_SECRET=[my-jwt-secret]
APP_HOST=0.0.0.0
NODE_PORT=1337

# Database postgres
DATABASE_CLIENT=postgres
DATABASE_HOST=127.0.0.1
DATABASE_PORT=5432
DATABASE_NAME=[my-database-name-as-defined-when-opening-and-setting-up-pgAdmin 4]
DATABASE_USERNAME=[my-database-username-as-defined-when-opening-and-setting-up-pgAdmin 4]
DATABASE_PASSWORD=[my-password-as-defined-when-opening-and-setting-up-pgAdmin 4]
DATABASE_SSL=false

Then make sure the connection is correct in config\env\production\database.js. The following is the content of this file after I got it working:

    // path: ./config/env/production/database.js

    module.exports = ({ env }) => ({
        connection: {
          client: 'postgres',
          connection: {
            host: env('DATABASE_HOST'),
            port: env.int('DATABASE_PORT'),
            database: env('DATABASE_NAME'),
            user: env('DATABASE_USERNAME'),
            password: env('DATABASE_PASSWORD'),
            ssl: false
            // ssl: {
            //   ca: env('DATABASE_SSL')
            // },
          },
          debug: false
        },
      });

Now when executing the command: NODE_ENV=production npm run start, there is no errors in the terminal, and the following message displays:

One more thing…
Create your first administrator :computer: by going to the administration panel at:

┌───────────────────────────┐
http://0.0.0.0:1337/admin

However, when navigating to this url, the browser does not load the page. Instead, it displays the error message:

This site can’t be reached

The web page at http://0.0.0.0:1337/admin might be temporarily down or it may have moved permanently to a new web address.

ERR_ADDRESS_INVALID

I will probably make this into a new post/question.