How to change default schema like `?schema={schema-name}`?
I have been set the schema of Prisma w/
DATABASE_URL. (e.g. postgres://....../db-name?schema=schema-name)
But On Prisma Postgres, It only connects to public schema.
How can I do?2 Replies
You opted for speed from this bot. Let's conjure up an answer right now!
To change the default schema Prisma uses in PostgreSQL, you typically add the
- For classic Prisma Client:
Prisma Docs: PostgreSQL Connection URL
Prisma GitHub Issue #28128
Prisma Discussions: Schema in Driver Adapters
?schema={schema-name} parameter to your DATABASE_URL. For example:
This should make Prisma use my_schema instead of the default public schema for all queries and migrations. However, recent changes in Prisma (especially when using the new prisma-client with driver adapters like @prisma/adapter-pg) have introduced inconsistencies: the schema parameter in the connection string is sometimes ignored, and Prisma defaults to the public schema unless you explicitly set the schema in the adapter configuration.
Solution for Driver Adapters (e.g., PrismaPg):
If you are using the new Prisma Client with driver adapters, you must explicitly set the schema in the adapter options:
Without this, Prisma will default to the public schema, even if your connection string specifies a different schema. This is a known issue and is being discussed in the community. For raw queries, you may also need to set the search_path using the options parameter in your connection string, like so:
Summary:- For classic Prisma Client:
?schema=my_schema in DATABASE_URL works.
- For new Prisma Client with driver adapters: set the schema in the adapter options, not just in the URL.
References:Prisma Docs: PostgreSQL Connection URL
Prisma GitHub Issue #28128
Prisma Discussions: Schema in Driver Adapters
Hey!
I assume you already have created a schema by running the command:
CREATE SCHEMA IF NOT EXISTS schema_name;
Once you do that you can provide a connection string like:
postgres://....../db-name?options=-csearch_path=schema_name which should now use the new schema as default.