Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: npx medusa db:setup fails #10105

Open
amatosc opened this issue Nov 14, 2024 · 8 comments
Open

[Bug]: npx medusa db:setup fails #10105

amatosc opened this issue Nov 14, 2024 · 8 comments

Comments

@amatosc
Copy link

amatosc commented Nov 14, 2024

Package.json file

{
  "name": "medusa-starter-default",
  "version": "0.0.1",
  "description": "A starter for Medusa projects.",
  "author": "Medusa (https://medusajs.com)",
  "license": "MIT",
  "keywords": [
    "sqlite",
    "postgres",
    "typescript",
    "ecommerce",
    "headless",
    "medusa"
  ],
  "scripts": {
    "build": "medusa build",
    "seed": "medusa exec ./src/scripts/seed.ts",
    "start": "medusa start",
    "dev": "medusa develop",
    "test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
    "test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
    "test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit"
  },
  "dependencies": {
    "@medusajs/admin-sdk": "latest",
    "@medusajs/cli": "latest",
    "@medusajs/framework": "latest",
    "@medusajs/medusa": "latest",
    "@mikro-orm/core": "5.9.7",
    "@mikro-orm/knex": "5.9.7",
    "@mikro-orm/migrations": "5.9.7",
    "@mikro-orm/postgresql": "5.9.7",
    "awilix": "^8.0.1",
    "pg": "^8.13.0"
  },
  "devDependencies": {
    "@medusajs/test-utils": "latest",
    "@mikro-orm/cli": "5.9.7",
    "@swc/core": "1.5.7",
    "@swc/jest": "^0.2.36",
    "@types/jest": "^29.5.13",
    "@types/node": "^20.0.0",
    "@types/react": "^18.3.2",
    "@types/react-dom": "^18.2.25",
    "jest": "^29.7.0",
    "prop-types": "^15.8.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "ts-node": "^10.9.2",
    "typescript": "^5.6.2",
    "vite": "^5.2.11"
  },
  "engines": {
    "node": ">=20"
  }
}

Node.js version

v20.11.1

Database and its version

PostgreSQL 15.6 on aarch64-unknown-linux-gnu, compiled by gcc (GCC) 13.2.0, 64-bit

Operating system name and version

MacOs 15.1 (24B83)

Browser name

Safari

What happended?

First create a new project skipping the bd creation

➜  /tmp npx create-medusa-app@latest --skip-db
? What's the name of your project? my-medusa-store
? Would you like to create the Next.js storefront? You can also create it later No
🚀 Starting project setup, this may take a few minutes.
✔ Created project directory
✔ Installed Dependencies
✔ Project Built
✔ Finished Preparation
✔ Project Prepared

when trying to initiate the BD, the commands fails. (I added the DATABASE_URL to the .env file)

➜  my-medusa-store npx medusa db:setup --db "postgres"
info:    Connection established with the database "postgres"
error:   DbHandler exited
error: DbHandler exited
    at /private/tmp/my-medusa-store/node_modules/pg/lib/client.js:535:17
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async dbExists (/private/tmp/my-medusa-store/node_modules/@medusajs/utils/src/pg/index.ts:48:18)
    at async dbCreate (/private/tmp/my-medusa-store/node_modules/@medusajs/medusa/src/commands/db/create.ts:138:7)
    at async main (/private/tmp/my-medusa-store/node_modules/@medusajs/medusa/src/commands/db/setup.ts:14:21) {
  length: 44,
  severity: 'FATAL',
  code: 'XX000',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: undefined,
  line: undefined,
  routine: undefined
}

Expected behavior

Create database

Actual behavior

Fails

Link to reproduction repo

not apply

@trinhcongduc
Copy link

Do you sure you have correct postgresql URL and it's ready to connect ? And show your medusa-config.ts file

@amatosc
Copy link
Author

amatosc commented Nov 15, 2024

Do you sure you have correct postgresql URL and it's ready to connect ? And show your medusa-config.ts file

Yes, actually I've tried

npx create-medusa-app@latest --db-url "postgresql://postgres.user:[email protected]:PORT/postgres"

and it works (the db-url is the same that I have in my .env file)

medusa-config.ts

➜  my-medusa-store-skipping-db cat medusa-config.ts
import { loadEnv, defineConfig } from '@medusajs/framework/utils'

loadEnv(process.env.NODE_ENV || 'development', process.cwd())

module.exports = defineConfig({
  projectConfig: {
    databaseUrl: process.env.DATABASE_URL,
    http: {
      storeCors: process.env.STORE_CORS!,
      adminCors: process.env.ADMIN_CORS!,
      authCors: process.env.AUTH_CORS!,
      jwtSecret: process.env.JWT_SECRET || "supersecret",
      cookieSecret: process.env.COOKIE_SECRET || "supersecret",
    }
  }
})

@trinhcongduc
Copy link

trinhcongduc commented Nov 15, 2024

Let's try connect DB by command line first: to make sure your postgresql url is correct .

psql -h aws-0-eu-west-3.pooler.supabase.com -p YOUR_PORT

By the way, make sure there is no space on your .env parameter's value

@amatosc
Copy link
Author

amatosc commented Nov 15, 2024

I did verify the connection before :( , what really surprises me is that this one works!

npx create-medusa-app@latest --db-url "postgresql://postgres.user:[email protected]:PORT/postgres"

No spaces on the .env file
Screenshot 2024-11-15 at 15 19 38

Connection proof
Screenshot 2024-11-15 at 15 20 08

@trinhcongduc
Copy link

It's quite strange!
If your Postgresql password include special characters, it will a bit different define them on your .env file . Take a look
image

Can you try it ?

@olivermrbl
Copy link
Contributor

The error message does indeed suggest an issue with the PG connection. Someone reported a similar scenario with a Supabase connection string here. This is worth a shot!

@olivermrbl olivermrbl self-assigned this Nov 15, 2024
@amatosc
Copy link
Author

amatosc commented Nov 18, 2024

It's quite strange! If your Postgresql password include special characters, it will a bit different define them on your .env file . Take a look image

Can you try it ?

My password doesn’t contain any special characters; it only includes uppercase and lowercase letters as well as numbers. I understand this might be a lot to ask, but if you could test with a Supabase database, you would see the issue for yourself. I’m using the default user and the password generated by Supabase (just create a new project)

@amatosc
Copy link
Author

amatosc commented Nov 18, 2024

The error message does indeed suggest an issue with the PG connection. Someone reported a similar scenario with a Supabase connection string here. This is worth a shot!

To be honest, I don’t think the issue is with the connection. As you can see in the first log:

info:    Connection established with the database "postgres"

I also tried the solution mentioned in that post, but it still didn’t work (same error). I found it strange that when running npx medusa db:setup, it required the DATABASE_URL in the .env file but then still asked for the database separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants