#How to choose right between prod and dev DATABASE_URL when using 'prisma db push'

4 messages · Page 1 of 1 (latest)

marsh hollow
#

Noob question:

So I have a NextJS project with Prisma. Currently I have db.ts file with this code: ```ts
export const db =
globalForPrisma.prisma ??
new PrismaClient({
log: env.NODE_ENV === "development" ? ["error"] : ["error"],
datasourceUrl:
env.NODE_ENV === "development" ? env.DATABASE_URL_DEV : env.DATABASE_URL,
});

if (env.NODE_ENV !== "production") globalForPrisma.prisma = db;```

This is perfect cause it chooses the right DATABASE_URL depending if im dev or prod. However, when I run prisma db push it chooses the one I have in prisma.schema. How would I solve this so it uses the dev one when pushing locally and prod one when in prod? Whats the best practise here?

#

my .envfile is has the two environment variables called DATABASE_URL_DEV and DATABASE_URL as you can see above.

grand lotus
#

Hi @marsh hollow 👋
The best practise would be to have two .env files. One for development and one for production. You can create a .env.development file for your development environment and a .env.production file for your production environment. Each file should contain the appropriate DATABASE_URL for its environment.

You could then use dotenv-cli to manage these files and define which env file should be used when running migration commands.

The detailed guide with step by step instructions can be found here:
https://www.prisma.io/docs/orm/more/development-environment/environment-variables/using-multiple-env-files

Prisma

Learn how to set up a dedicated testing environment using multiple .env files.

marsh hollow