I am using nestjs, prisma and postgres. The db and repo are deployed to production using…
- User
I am using nestjs, prisma and postgres. The db and repo are deployed to production using railway, how can I fill the database with all of the tables
- Assistant
and initial data after deployment?
There are several ways to populate a Postgres instance with initial data:
-
Using a seed script: You can create a script that populates the database with initial data. This script can be executed automatically after the database is provisioned. For example, you can use
psqlcommand to execute the SQL commands that populate the database. -
Using Prisma Migrations: Prisma Migrations allow you to version control your database schema and data. You can create a migration file that includes both the schema changes and data changes (using the
upfunction of the migration file). When you deploy your app, the Prisma Migrations CLI can run these migrations automatically. -
Using a package like
typeorm-seeding: TypeORM Seeding allows you to easily generate and populate your database with test data. You can set up your seeding configuration file to populate your database on startup, or you can execute the seeding process manually.
Once you have set up your chosen method of populating the database with initial data, you can configure it to run automatically after deployment using your deployment platform's configuration options (for example, using the Railway CLI).
-
- User
I don't want to add data, just create the tables from the prisma schema
- Assistant
If you have already defined your database schema in Prisma and have generated the Prisma Client, you can use the Prisma Migrate CLI to create the database tables from the schema in production.
Assuming you've already deployed your Prisma schema to Railway, you can run the following steps:
- Install the Prisma Migrate CLI by running
npm install prisma --save-devoryarn add prisma --dev. - Generate a migration by running
npx prisma migrate dev --name {migration name}oryarn prisma migrate dev --name {migration name}. This will generate a file under theprisma/migrations/directory. - Apply the changes by running
npx prisma migrate deployoryarn prisma migrate deploy. This will create the tables in your production database using the schema you defined in Prisma. - Verify that the tables and constraints are created in your production database using a tool like
psqlor a DB visualizer.
These steps should create the tables based on your Prisma schema in your production environment. If you want to modify the schema later on, you can add additional migrations and apply those changes by running the command again.
- Install the Prisma Migrate CLI by running