Database

Learn how to set up the production database and configure GitHub Actions to sync migrations with the production database.

Production Database

I've chosen to use Neon, a Serverless Postgres solution, as the database provider. However, feel free to use any provider you prefer. Neon offers a free tier, which is ideal for getting started.

To set up the production database, follow these steps:

Create an account

Go to the Neon website, create an account, and log in.

Create a new project

Choose a project name, database name, and region.

Click on the Create Project button.

Get the connection string

Navigate to the Dashboard tab.

Below the Connection details section, click on the Connection string dropdown and select Prisma.

Click the .env tab.

Click the Show password button to reveal the database password.

Copy the database connection string.

Sync the database schema

Duplicate the DATABASE_URL in the .env file and comment out the local database connection string.

Update the uncommented DATABASE_URL in the .env file with the connection string you copied.

In order to sync the production database schema with the Prisma schema, run the following command in the root of your project:

pnpm db:push

Never do this for future migrations. This is only for the initial setup. We will setup Github Actions to automate the migration process below.

Update the .env file to the original state

Copy the production database string from the .env file and save it in a safe place for future use when we deploy the app.

Remove the production database string from your .env file and uncomment the local database string.

Save the changes.

Github Actions

Whenever we make changes to the database schema, we need to sync the changes to the production database.

We can streamline this process by using GitHub Actions to automate the database migration process.

The Github Actions workflow is already setup in the .github/workflows/deploy-prisma.yml file.

All you need to do is add the DATABASE_URL, NEXT_PUBLIC_APP_URL and RESEND_KEY secrets to your repository secrets on GitHub.

Go to your repository on Github

Click on the Settings tab.

Click on the Secrets and variables tab.

Click on Actions.

Click on the New repository secret button.

Add the following secrets:

  • DATABASE_URL - The connection string to your production database.
  • NEXT_PUBLIC_APP_URL - The URL to your production app. If you don't have a domain yet, you can use any URL such as https://google.com.
  • RESEND_KEY - The Resend API key.

The value of NEXT_PUBLIC_APP_URL and RESEND_KEY doesn't matter. We only need to add them to the repository secrets to avoid an error in the Github Actions workflow.

That's it! Now, whenever you push any database schema changes to the main branch, the Github Actions workflow will run and sync the changes to the production database.

You can read more about how to change the database schema and create migrations here.

Last updated on