You are installing Medusa, or setting up an existing project on a new machine, and everything stops at the migration step. Sometimes create-medusa-app fails while “Running Migrations”, sometimes npx medusa db:migrate prints a long stack trace, and sometimes it just hangs with no output at all. In this tutorial, we will fix the Medusa db:migrate error by reading the exact message, matching it to its cause and fixing the database connection.
I reproduced each error below on a real Medusa 2.23.2 project with PostgreSQL 16, so the messages in the screenshots are exactly what you will see. If you have not installed Medusa yet, start with my guide on how to install Medusa v2 step by step.
Contents
- How npx medusa db:migrate connects to the database
- Error 1: ECONNREFUSED (PostgreSQL is not running)
- Error 2: password authentication failed for user
- Error 3: database “…” does not exist
- The fix: create the database, then run db:migrate again
- Other causes of Medusa migration errors
- How to avoid migration errors next time
- Wrapping up
How npx medusa db:migrate connects to the database
The db:migrate command runs every pending migration of every module, syncs the link tables between modules and then runs migration scripts (such as the seed script of the starter). To do that, it reads projectConfig.databaseUrl in medusa-config.ts, which by default comes from the DATABASE_URL variable in apps/backend/.env:
DATABASE_URL=postgres://[user]:[password]@[host]:[port]/[database]
Almost every migration failure is one of these five parts being wrong, or the PostgreSQL server not running. The trick is to read which part the error message complains about. Run the command from the backend folder, and add --verbose when you see no useful output:
cd apps/backend npx medusa db:migrate --verbose
Error 1: ECONNREFUSED (PostgreSQL is not running)
If PostgreSQL is stopped, or listening on another port, db:migrate does not fail right away. It keeps retrying, so without --verbose it looks frozen. With verbose logging you see the real reason:
ECONNREFUSED ... port 5432 means nothing is listening on that host and port. The “Knex: Timeout acquiring a connection” line is only a consequence of it. Fix:
- Check the server:
pg_isready -h localhost -p 5432. If it saysno response, start PostgreSQL (sudo systemctl start postgresqlon Ubuntu,brew services start postgresql@16on macOS). - If PostgreSQL runs in Docker, make sure the port is published with
-p 5432:5432, and uselocalhostin the URL when Medusa runs outside the container. - If your PostgreSQL uses another port, put it in the URL (
@localhost:5433/).
Error 2: password authentication failed for user
This one is explicit. PostgreSQL is reachable, but the user name or password in DATABASE_URL is wrong (error code 28P01):
Fix the credentials in apps/backend/.env, or reset the password in PostgreSQL:
sudo -u postgres psql -c "ALTER ROLE medusa WITH PASSWORD 'new-strong-password';"
Watch out for special characters. If your password contains @, :, / or #, URL-encode them in DATABASE_URL (for example @ becomes %40), otherwise the URL is parsed incorrectly and you get confusing authentication errors. A related message, SASL: SCRAM-SERVER-FIRST-MESSAGE: client password must be a string, usually means the password is missing entirely, for example because DATABASE_URL is not loaded from .env.
Error 3: database “…” does not exist
The server and credentials are fine, but the database name at the end of the URL has not been created yet (error code 3D000). This is very common when you clone an existing Medusa project onto a new machine:
The fix: create the database, then run db:migrate again
Medusa has its own command to create the database: npx medusa db:create. Pass the name with --db, and add --no-interactive if you are on a server. Then run the migrations again:
pg_isready -h localhost -p 5432 npx medusa db:create --db medusa_store --no-interactive npx medusa db:migrate
Notice that db:create also wrote DB_NAME=medusa_store into your .env file. A successful run ends with Migrations completed, Links sync completed and, on a starter project, Migration scripts completed (that is the script that seeds the default region and sample data). You can also create the database with plain PostgreSQL (createdb -O medusa medusa_store) if you prefer.
Finally, start the server with npx medusa develop and open the admin. If the store settings load, the database is fully migrated:
Other causes of Medusa migration errors
SSL errors with a hosted PostgreSQL
Hosted databases often require SSL, while a local PostgreSQL does not support it. If the error mentions SSL, either append ?sslmode=disable to the URL for a server without SSL, or configure databaseDriverOptions in medusa-config.ts with ssl: { rejectUnauthorized: false } for a server that requires SSL with a self-signed certificate.
permission denied to create database / extension
The database user needs enough privileges. Give it CREATEDB (ALTER ROLE medusa CREATEDB;) or let it own the database. The postgres superuser always works for local development.
Migrations fail halfway and leave a mess
Run npx medusa db:migrate --all-or-nothing so that a failed migration reverts the ones already applied in that run. On a fresh project, it is often faster to drop the database, recreate it and migrate again.
These are the same database checks I use for other platforms. For example, a wrong database connection is also behind many cases of the Magento 2 500 internal server error.
See the official Medusa database errors troubleshooting guide and the medusa db commands reference for more options.
How to avoid migration errors next time
Once the database works, a few habits keep db:migrate boring:
- Test the connection string on its own. Before blaming Medusa, run
psql "postgres://medusa:password@localhost:5432/medusa_store" -c "select 1"with the exact value ofDATABASE_URL. Ifpsqlcannot connect, Medusa cannot either. - Keep one
.envper environment. Most “it works on my machine” migration errors come from a localDATABASE_URLcopied to a server. On hosting platforms, setDATABASE_URLas an environment variable instead of committing.env. - Run migrations as a deploy step. After every Medusa or plugin update, run
npx medusa db:migratebefore starting the new version, so the code never runs against an old schema. - Back up before upgrading. A quick
pg_dumpof the database before a major update lets you roll back if a migration fails halfway. - Read the first error, not the last. The stack trace after the first
error:line is mostlypg-protocolinternals. The useful part is the sentence on the first line and the PostgreSQLcode(such as28P01for a wrong password or3D000for a missing database).
Wrapping up
When npx medusa db:migrate fails, read the first error line: ECONNREFUSED means PostgreSQL is not running or the host/port is wrong, password authentication failed means wrong credentials, and database ... does not exist means you need to run npx medusa db:create first. Fix DATABASE_URL, run the migration again, and Medusa will continue where it stopped.
Still stuck with a different migration error? Paste the first few lines of the output in the comments (remove your password first) and I will take a look.






