Add a free Heroku Postgresql database to your serverless web app

Heroku offers ☁️ remotely hosted Postgresql databases on all tiers. This note focuses on using the DB only without hosting the app on Heroku.

  • Create a Heroku account and create an app from the Heroku dashboard.
  • Open your app, go to "Resources", search for "Heroku Postgres" in the "Add-ons" section, add it.
  • Once added, click the "Heroku Postgres" item, which will take you to your Datastores Dashboard.
  • Go to the "Settings" page. Click "View Credentials", copy the URI value, and you can start connecting to the DB.

The DB URL should look like this:

postgres://{RANDOM_USER_ID_AND_PASSWORD}@{AWS_CONTAINER_SUBDOMAIN}.amazonaws.com:5432/{RANDOM_DB_NAME}
caution

Your DB URL is private. Make sure not to accidentally commit it to git/version control by storing it in an env config file and adding the file to .gitignore.

Since I use Prisma as an ORM for my Next.js web app^[Prisma is framework-agnostic; you can use it with Next, Remix, Astro, etc.],I use the DB URL in my schema like this.

# .env
PRISMA_DB_URL=postgres://...
// prisma.schema
datasource db {
  provider = "postgresql"
  url      = env("PRISMA_DB_URL")
}

// ... other stuff

Documentation:

In:

Links to this note

  • Est ex deserunt esse ut pariatur quis fugiat id velit commodo

    Ut anim fugiat laboris et eiusmod aliquip.

  • Est ex deserunt esse ut pariatur quis fugiat id velit commodo

    Ut anim fugiat laboris et eiusmod aliquip.

  • Est ex deserunt esse ut pariatur quis fugiat id velit commodo

    Ut anim fugiat laboris et eiusmod aliquip.

  • Est ex deserunt esse ut pariatur quis fugiat id velit commodo

    Ut anim fugiat laboris et eiusmod aliquip.

  • Est ex deserunt esse ut pariatur quis fugiat id velit commodo

    Ut anim fugiat laboris et eiusmod aliquip.

  • Est ex deserunt esse ut pariatur quis fugiat id velit commodo

    Ut anim fugiat laboris et eiusmod aliquip.

  • Est ex deserunt esse ut pariatur quis fugiat id velit commodo

    Ut anim fugiat laboris et eiusmod aliquip.