medusajs / medusa

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

Digital Ocean backend deployment failed with "Failed to establish a connection to PostgreSQL" #8695

Open volfcan opened 3 weeks ago

volfcan commented 3 weeks ago

Bug report

Describe the bug

I get the following deployment error on digital ocean. Build was successful but deployment failed.

Error: Failed to establish a connection to PostgreSQL. Please ensure the following is true and try again:
2024-08-21T11:44:44.517164089Z       - You have a PostgreSQL database running
2024-08-21T11:44:44.517171443Z       - You have passed the correct credentials in medusa-config.js
2024-08-21T11:44:44.517176289Z       - You have formatted the database connection string 

System information

Medusa version (including plugins):

  "dependencies": {
    "@medusajs/admin": "^7.1.15",
    "@medusajs/cache-inmemory": "1.8.11",
    "@medusajs/cache-redis": "^1.9.2",
    "@medusajs/event-bus-local": "1.9.9",
    "@medusajs/event-bus-redis": "^1.8.14",
    "@medusajs/file-local": "1.0.4",
    "@medusajs/medusa": "^1.20.9",
    "medusa-fulfillment-manual": "1.1.41",
    "medusa-interfaces": "1.3.10",
    "medusa-payment-manual": "1.0.25",
    "medusa-payment-stripe": "6.0.10",
  },

Steps to reproduce the behavior

  1. Go to 'repository'
  2. Fork it
  3. Deploy on digital ocean app platform
  4. See error

Expected behavior

No error on deployment

Screenshots

databases up and running in digital ocean¡

Screenshot 2024-08-21 at 15 12 01

If applicable, add screenshots to help explain your problem

Code snippets

medusa-config.jsfile:

const dotenv = require("dotenv");

let ENV_FILE_NAME = "";
switch (process.env.NODE_ENV) {
  case "production":
    ENV_FILE_NAME = ".env.production";
    break;
  case "staging":
    ENV_FILE_NAME = ".env.staging";
    break;
  case "test":
    ENV_FILE_NAME = ".env.test";
    break;
  case "development":
  default:
    ENV_FILE_NAME = ".env";
    break;
}

try {
  dotenv.config({ path: process.cwd() + "/" + ENV_FILE_NAME });
} catch (e) {}

// CORS when consuming Medusa from admin
const ADMIN_CORS =
  process.env.ADMIN_CORS || "http://localhost:7000,http://localhost:7001";

// CORS to avoid issues when consuming Medusa from a client
const STORE_CORS = process.env.STORE_CORS || "http://localhost:8000";

const DB_USERNAME = process.env.DB_USERNAME;
const DB_PASSWORD = process.env.DB_PASSWORD;
const DB_HOST = process.env.DB_HOST;
const DB_PORT = process.env.DB_PORT;
const DB_DATABASE = process.env.DB_DATABASE;

const DATABASE_URL =
  `postgres://${DB_USERNAME}:${DB_PASSWORD}` +
  `@${DB_HOST}:${DB_PORT}/${DB_DATABASE}`;

const REDIS_URL = process.env.REDIS_URL || "redis://localhost:6379";

const plugins = [
  `medusa-fulfillment-manual`,
  `medusa-payment-manual`,
  {
    resolve: `@medusajs/file-local`,
    options: {
      upload_dir: "uploads",
    },
  },
  {
    resolve: "@medusajs/admin",
    /** @type {import('@medusajs/admin').PluginOptions} */
    options: {
      autoRebuild: true,
      develop: {
        open: process.env.OPEN_BROWSER !== "false",
      },
    },
  },
];

const modules = {
  eventBus: {
    resolve: "@medusajs/event-bus-redis",
    options: {
      redisUrl: REDIS_URL,
    },
  },
  cacheService: {
    resolve: "@medusajs/cache-redis",
    options: {
      redisUrl: REDIS_URL,
    },
  },
};

/** @type {import('@medusajs/medusa').ConfigModule["projectConfig"]} */
const projectConfig = {
  jwt_secret: process.env.JWT_SECRET || "supersecret",
  cookie_secret: process.env.COOKIE_SECRET || "supersecret",
  store_cors: STORE_CORS,
  database_url: DATABASE_URL,
  worker_mode: process.env.MEDUSA_WORKER_MODE,
  admin_cors: ADMIN_CORS,
  // Uncomment the following lines to enable REDIS
  redis_url: REDIS_URL,
};

/** @type {import('@medusajs/medusa').ConfigModule} */
module.exports = {
  projectConfig: {
    database_extra: { ssl: { rejectUnauthorized: false } },
    redis_url: process.env.REDIS_URL,
  },
  plugins,
  modules: {
    cacheService: {
      resolve: "@medusajs/cache-redis",
      options: {
        redisUrl: process.env.CACHE_REDIS_URL,
        ttl: 30,
      },
    },
    eventBus: {
      resolve: "@medusajs/event-bus-redis",
      options: {
        redisUrl: process.env.EVENTS_REDIS_URL,
      },
    },
  },
};

digital ocean deployment log file: medusa-backend2-deploy.txt

sradevski commented 3 weeks ago

@volfcan can you please make sure that this is a Medusa bug and not a misconfigured DB URL, for example? Please provide more information on what you tried when debugging (eg. connecting to the same DB connection string manually) before we can start looking into it.

volfcan commented 3 weeks ago

trying to connect to same connection string on my local machine was successful. i am thinking about that he cause of the issue is probably medusa-config.js file