medusajs / medusa

Building blocks for digital commerce
https://medusajs.com
MIT License
25.03k stars 2.5k forks source link

Unable to connect to a remote DB #4881

Closed yasirmturk closed 1 year ago

yasirmturk commented 1 year ago

Preliminary Checks

Issue Summary

Unable to connect to a remote db, localhost works but none of these work, i have tried

Error starting server Error: Failed to establish a connection to PostgreSQL. Please ensure the following is true and try again:
      - You have a PostgreSQL database running
      - You have passed the correct credentials in medusa-config.js
      - You have formatted the database connection string correctly. See below:
      "postgres://[username]:[password]@[host]:[post]/[db_name]" - If there is no password, you can omit it from the connection string

      connect ECONNREFUSED 52.57.171.9:5432
    at handlePostgresDatabaseError (/home/zbrdst/nodevenv/shop.zabrdast.com/16/lib/node_modules/@medusajs/utils/dist/common/handle-postgres-database-error.js:17:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

How can this issue be resolved?

1. 2. 3. ...

Are you interested in working on this issue?

shahednasser commented 1 year ago

Thank you for submitting this issue! Can you specify:

  1. What command did you use to create your Medusa backend project? (create-medusa-app or medusa new)
  2. How did you set up your connection URL and if possible sharing how it's set in .env (feel free to omit sensitive information here like the password)
yasirmturk commented 1 year ago
  1. I used create-medusa-app and git clone the repo, both are almost same
  2. First it complaint about ssl so I added ?sslmode=require

Anyway, I just slept over it and today it start working on local; without any changes literally :D but it is still not working on server. This is my .env file

NODE_ENV=development
DATABASE_TYPE=postgres
DATABASE_URL=postgres://yasirmturk:<password>@ep-odd-breeze-80644673.eu-central-1.aws.neon.tech/neondb?sslmode=require
shahednasser commented 1 year ago

Sorry this may be a silly question, but did you make sure that your server is using the correct environment variables (similar to local)? Because I'm not sure what the difference should be for it to not work there 😅

Also can you ensure that you're using the same Medusa versions and everything both locally and your server?

yasirmturk commented 1 year ago

That is actually a very good question. Cpanel provides its own interface to configure env variables just like heroku, i have provided same values there and also in .env file..

and the app seems to be initialized fine, for example it does not complain about the JWT or Cookie secret.. Just unable to connect to the db server. Another fishy thing is that for 1 hit the whole app runs 3 times as I can see in the error logs attached here

I am also checking with the server guys but there should not be any restriction regarding outgoing connections... Is there any way to access the raw logs? can I enable debug logs or something to dig deep into the problem?

Screenshot 2023-08-30 at 7 21 54 PM
shahednasser commented 1 year ago

The JWT and Cookie secrets aren't an indication since a temporary value can be used if not provided in development, and it seems like you're using the medusa develop command, otherwise the logs output would be different. I would start with ensuring that the environment variables are actually loaded.

You can specify a log file by setting the LOG_FILE environment variable, but it's currently only supported through environment variables 😅

yasirmturk commented 1 year ago

So, I specified LOG_FILE=log.txt and this is what I get in the file

{"level":"info","message":"Using fake Redis","timestamp":"2023-08-31 08:35:37"}
{"level":"warn","message":"Local Event Bus installed. This is not recommended for production.","timestamp":"2023-08-31 08:35:37"}
{"level":"info","message":"Using fake Redis","timestamp":"2023-08-31 08:35:40"}
{"level":"warn","message":"Local Event Bus installed. This is not recommended for production.","timestamp":"2023-08-31 08:35:40"}
{"level":"info","message":"Using fake Redis","timestamp":"2023-08-31 08:35:43"}
{"level":"warn","message":"Local Event Bus installed. This is not recommended for production.","timestamp":"2023-08-31 08:35:43"}
shahednasser commented 1 year ago

That's not really an error. It just means that you should be using a production redis module and ensure you've setup redis configuraitons.

yasirmturk commented 1 year ago

So, the port is blocked on the nodejs app for cpanel, the issue has been resolved, thank you for you help but you might want to add it to you documentation.

shahednasser commented 1 year ago

Sure, thanks for pointing it out!

fffilps commented 11 months ago

@yasirmturk would you mind helping me deploy using cpanel. Im also using it and im struggling to set it up, especially the postgresql portion.

devwatts commented 10 months ago
  1. I used create-medusa-app and git clone the repo, both are almost same
  2. First it complaint about ssl so I added ?sslmode=require

Anyway, I just slept over it and today it start working on local; without any changes literally :D but it is still not working on server. This is my .env file

NODE_ENV=development
DATABASE_TYPE=postgres
DATABASE_URL=postgres://yasirmturk:<password>@ep-odd-breeze-80644673.eu-central-1.aws.neon.tech/neondb?sslmode=require

This helped me , Thanks!

storefronticu-admin commented 9 months ago

Hi there if you are using the --db url option make sure that the db url is incased in "" or else it will fail