Strapi and postgres: ECONNREFUSED

Hi there,

I keep getting the following error when deploying my strapi instance:

Apr 23 12:19:15 PM  [2022-04-23 19:19:15.670] debug: ⛔️ Server wasn't able to start properly.
Apr 23 12:19:15 PM  [2022-04-23 19:19:15.672] error: connect ECONNREFUSED 127.0.0.1:5432
Apr 23 12:19:15 PM  error Command failed with exit code 1.
Apr 23 12:19:15 PM  Error: connect ECONNREFUSED 127.0.0.1:5432    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16)

I just can’t deploy strapi with postgres despite following the instructions and the template repo.
Could I please have some guidance here?

Thanks in advance

1 Like

Hey, I have been dealing with a similar issue here: Strapi Postgres Connection Pool breaks connection - Database-SQL - Strapi Community Forum

Would love to get behind this!

@daawascript Welcome to the Render community!

The line in that stack trace that is key here is:

Apr 23 12:19:15 PM  Error: connect ECONNREFUSED 127.0.0.1:5432    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1161:16)

specifically, the 127.0.0.1 part - Postgres isn’t running on that IP address so you should make sure you are using the correct internal string/hostname for your database, typically via the DATABASE_URL - it’s worth checking config/database.js and make sure it’s what you expect here.

I’m not sure this is related to the original topic.

Hi John,

I appreciate your input. I thought that this IP looked out of place here, thanks for confirming.

I double checked my DATABASE_URL variable and database.js files and they are correct. I can confirm that DATABASE_URL is the internal connection string. I even tried using the external connection string. It does work when connecting via SSH from my machine and I can access this db. It gives the same error when connecting from render.

Here’s the code inside config/production/database.js

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

module.exports = ({ env }) => {
  const { host, port, database, user, password } = parse(env("DATABASE_URL"));

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

That looks exactly as it should do - it would be worth checking what you have in config/database.js then - it could be that for some reason that is being used instead of config/production/database.js - you could try changing the port number in that file and seeing if the service uses that new port when it tries to start, if it does then that’s the problem.

John B

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.