Skip to content

Prisma Migrate: Error creating shadow database #4571

Closed

Description

@albertoperdomo

Problem

Users run into this issue if their database user has no privileges to create databases (MySQL / Postgres). This happens because Migrate tries to create a shadow database under the hood.

> DATABASE_URL="postgres://prismatest:prismatest@localhost:5432/prismatest" npx prisma migrate dev --preview-feature
Environment variables loaded from prisma/.env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "prismatest", schema "public" at "localhost:5432"
2020-12-08 17:22:21.346 WET [11179] ERROR:  relation "_prisma_migrations" does not exist at character 126
2020-12-08 17:22:21.346 WET [11179] STATEMENT:  SELECT "id", "checksum", "finished_at", "migration_name", "logs", "rolled_back_at", "started_at", "applied_steps_count" FROM "_prisma_migrations" ORDER BY "started_at" ASC
2020-12-08 17:22:21.347 WET [11179] ERROR:  permission denied to create database
2020-12-08 17:22:21.347 WET [11179] STATEMENT:  CREATE DATABASE "prisma_migrations_shadow_database_8e443e23-bb18-4c98-8903-8777da7f625b"
Error: A migration failed when applied to the shadow database:
Database error: Error querying the database: db error: ERROR: permission denied to create database
   0: sql_migration_connector::flavour::postgres::sql_schema_from_migration_history
             at migration-engine/connectors/sql-migration-connector/src/flavour/postgres.rs:172
   1: sql_migration_connector::sql_database_migration_inferrer::calculate_drift
             at migration-engine/connectors/sql-migration-connector/src/sql_database_migration_inferrer.rs:59
   2: migration_core::api::DiagnoseMigrationHistory
             at migration-engine/core/src/api.rs:148

Some of the situations where this might be the case:

  • Running DBMS locally but user has insufficient privileges -> Possible resolution: grant privileges. We should improve the user-facing error and document this properly
  • Using a cloud-hosted DB for development, where creating additional databases is probably not supported -> Possible resolution: make the shadow DB URL configurable

Additional context

Activity

  1. added this to the 2.14.0 milestone on Dec 9, 2020
  2. Jolg42 commented on Dec 10, 2020

    @Jolg42
    Contributor
  3. albertoperdomo commented on Dec 16, 2020

    @albertoperdomo
    ContributorAuthor

    User-facing error message (draft):

    Prisma Migrate could not create the shadow database. Please make sure the database user has permission to create databases.  More info: https://pris.ly/d/migrate-shadow
    

    The link https://pris.ly/d/migrate-shadow should be added to Prisly and it can point to https://www.prisma.io/docs/concepts/components/prisma-migrate#shadow-database

  4. added a commit that references this issue on Dec 16, 2020
  5. Jolg42 commented on Dec 16, 2020

    @Jolg42
    Contributor
  6. 39 remaining items

  7. AuthorOfTheSurf commented on May 7, 2023

    @AuthorOfTheSurf

    I ran into this while attempting to set up a redwood app with an app-specific database user:

    I thought that this GRANT would be sufficient

    GRANT ALL PRIVILEGES ON DATABASE my_app_dev TO my_app;
    

    But it doesn't actually grant CREATEDB to the my_app user

    I needed to hop back into psql and use ALTER USDER:

    ALTER USER my_app CREATEDB;
    

    Can confirm that "Create DB" access has been granted with \du:

                                           List of roles
         Role name     |                         Attributes                         | Member of 
    -------------------+------------------------------------------------------------+-----------
     my_app            | Create DB                                                  | {}
     postgres          | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
    

    In the end I have a local postgres user called my_app that can do whatever it wants with my_app_dev database. So I am not reliant on the top-level user called postgres or <machine_name>. This was my intention

    P.S. Here is my connection string in case it may help anyone:

    # The password is "password" in my case
    DATABASE_URL=postgres://my_app:password@localhost:5432/my_app_dev
    
  8. nzorg commented on May 15, 2023

    @nzorg

    For anyone using MySQL, Prisma creates and drops a bunch of prisma_migrate_shadow_db_[UUID] databases. So if your user only has access to the app DB it will fail. Assuming your user configured in .env is johndoe, this will work:

    GRANT CREATE, DROP ON *.* TO johndoe@'%';

    I have a few issues with this. The command output is not clear at all, because it says "User johndoe was denied access on the database mydb", when in fact it had access to that db, but not to create other dbs. This should be better documented. And an even bigger concern, by allowing CREATE and DROP in all databases, we're compromising the security and not following least privilege principle. In MySQL there's no way to grant create and drop for a particular DB prefix, so the only way it can work is by allowing wildcard.

  9. Jolg42 commented on May 15, 2023

    @Jolg42
    Contributor

    @n5sgg Thanks for the feedback. I think in this case, which is probably not obvious in our docs, you could use the shadowDatabaseUrl property to define the shadow database name permanently. Meaning, you can then grant permissions for create and drop only for that database, eliminating the security risk of the wildcard.

    See https://www.prisma.io/docs/concepts/components/prisma-migrate/shadow-database#cloud-hosted-shadow-databases-must-be-created-manually

    datasource db {
      provider          = "mysql"
      url               = env("DATABASE_URL")
      shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
    }
  10. nzorg commented on May 15, 2023

    @nzorg

    Thanks @Jolg42, it worked fine. I've created a hello-prisma repo using mysql and docker-compose in case this helps anyone: https://github.com/n5sgg/hello-prisma

  11. LeCongNam commented on May 30, 2023

    @LeCongNam

    if you using mysql with docker. you must using Account root. I'm don't know why Prisma required Root Account. If using DB from Cloud Share cannot not fix :) Prisma recomment you using command

    $ prisma db push
  12. janpio commented on Jun 7, 2023

    @janpio
    Contributor

    You do not need to use a root account. You just need to use an account that can execute CREATE DATABASE. Otherwise you can define the shadowDatabaseUrl to an already existing database.

  13. isaacgr commented on Aug 14, 2023

    @isaacgr
  14. Jolg42 commented on Aug 14, 2023

    @Jolg42
  15. isaacgr commented on Aug 15, 2023

    @isaacgr
  16. coderunner86 commented on Jan 23, 2024

    @coderunner86

    You can also run npx prisma db push

  17. newerton commented on Apr 10, 2024

    @newerton

    I had the same problem with migration, downgrading to 5.9.1 worked.

    Error with version (5.12.1, 5.12.0, 5.11.0, 5.10.2, 5.10.1 and 5.10.0)

    Error: P1017
    Server has closed the connection.
    

    Error log

    postgresql 21:05:14.77 INFO  ==> ** Starting PostgreSQL **
    LOG:  pgaudit extension initialized
    LOG:  starting PostgreSQL 16.2 on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
    LOG:  listening on IPv4 address "0.0.0.0", port 5432
    LOG:  listening on IPv6 address "::", port 5432
    LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
    ERROR:  relation "_prisma_migrations" does not exist at character 126
    STATEMENT:  SELECT "id", "checksum", "finished_at", "migration_name", "logs", "rolled_back_at", "started_at", "applied_steps_count" FROM "_prisma_migrations" ORDER BY "started_at" ASC
    FATAL:  terminating connection due to administrator command
    LOG:  checkpoint starting: immediate force wait
    LOG:  checkpoint complete: wrote 7 buffers (0.0%); 1 WAL file(s) added, 0 removed, 0 recycled; write=0.003 s, sync=0.003 s, total=0.033 s; sync files=6, longest=0.002 s, average=0.001 s; distance=4258 kB, estimate=4258 kB; lsn=0/1D52BC0, redo lsn=0/1D52B88
    

    Postgres: https://artifacthub.io/packages/helm/bitnami/postgresql
    Version: 16.2.0

    Prisma Version

    Environment variables loaded from .env
    Environment variables loaded from src\_core\@shared\infrastructure\adapters\persistence\database\prisma\.env
    prisma                  : 5.9.1
    @prisma/client          : 5.12.1
    Computed binaryTarget   : windows
    Operating System        : win32
    Architecture            : x64
    Node.js                 : v18.19.1
    Query Engine (Node-API) : libquery-engine 23fdc5965b1e05fc54e5f26ed3de66776b93de64 (at node_modules\@prisma\engines\query_engine-windows.dll.node)
    Schema Engine           : schema-engine-cli 23fdc5965b1e05fc54e5f26ed3de66776b93de64 (at node_modules\@prisma\engines\schema-engine-windows.exe)
    Schema Wasm             : @prisma/prisma-schema-wasm 5.9.0-32.23fdc5965b1e05fc54e5f26ed3de66776b93de64
    Default Engines Hash    : 23fdc5965b1e05fc54e5f26ed3de66776b93de64
    Studio                  : 0.497.0
    
  18. ben-harris-5 commented on Jun 11, 2024

    @ben-harris-5

    @newerton I experienced the same issue too, and reverting to 5.9.1 also worked for me!

  19. sadeeabdullah commented on Nov 13, 2024

    @sadeeabdullah

    I was confronting the same issue.
    Here is the solution (https://satisfyhost.com/blog/prisma-migrate-could-not-create-the-shadow-database/) that
    i followed the blog and solved the problem in a moment.

  20. Abdulrehman3802 commented on Aug 12, 2025

    @Abdulrehman3802

    @n5sgg Thanks for the feedback. I think in this case, which is probably not obvious in our docs, you could use the shadowDatabaseUrl property to define the shadow database name permanently. Meaning, you can then grant permissions for create and drop only for that database, eliminating the security risk of the wildcard.

    See https://www.prisma.io/docs/concepts/components/prisma-migrate/shadow-database#cloud-hosted-shadow-databases-must-be-created-manually

    datasource db {
    provider = "mysql"
    url = env("DATABASE_URL")
    shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
    }

    If you encounter permission issues while creating a shadow database with Prisma, try adding this configuration in your schema.prisma. This resolves the problem as some hosting providers restrict DB users from creating new databases. Thanks to @Jolg42 for the helpful tip!

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions