Back to Drizzle Orm

Get Started with Drizzle and PlanetScale Postgres

src/content/docs/get-started/planetscale-postgres-new.mdx

latest3.2 KB
Original Source

import Tab from '@mdx/Tab.astro'; import Tabs from '@mdx/Tabs.astro'; import Npm from "@mdx/Npm.astro"; import Callout from '@mdx/Callout.astro'; import Steps from '@mdx/Steps.astro'; import AnchorCards from '@mdx/AnchorCards.astro'; import Breadcrumbs from '@mdx/Breadcrumbs.astro'; import Prerequisites from "@mdx/Prerequisites.astro"; import CodeTabs from "@mdx/CodeTabs.astro"; import FileStructure from '@mdx/get-started/FileStructure.mdx'; import InstallPackages from '@mdx/get-started/InstallPackages.mdx'; import ConnectPlanetScalePostgres from '@mdx/get-started/postgresql/ConnectPlanetScalePostgres.mdx' import CreateTable from '@mdx/get-started/postgresql/CreateTable.mdx' import SetupConfig from '@mdx/get-started/SetupConfig.mdx'; import ApplyChanges from '@mdx/get-started/ApplyChanges.mdx'; import RunFile from '@mdx/get-started/RunFile.mdx'; import QueryDatabase from '@mdx/get-started/QueryDatabase.mdx'; import SetupEnv from '@mdx/get-started/SetupEnv.mdx';

<Breadcrumbs/>

Get Started with Drizzle and PlanetScale Postgres

<Prerequisites> - **dotenv** - package for managing environment variables - [read here](https://www.npmjs.com/package/dotenv) - **tsx** - package for running TypeScript files - [read here](https://tsx.is/) - **PlanetScale Postgres** - PostgreSQL database platform - [read here](https://planetscale.com/docs/postgres) - **node-postgres** - package for querying your PostgreSQL database - [read here](https://node-postgres.com/) </Prerequisites> <Callout title='PlanetScale also offers MySQL' type='info'> Looking for MySQL? Check out our [PlanetScale MySQL guide](/docs/get-started/planetscale-new) </Callout>

PlanetScale offers both MySQL (Vitess) and PostgreSQL databases. This guide covers connecting to PlanetScale Postgres using the standard node-postgres driver.

For detailed instructions on creating a PlanetScale Postgres database and obtaining credentials, see the PlanetScale Postgres documentation.

<FileStructure/>

Step 1 - Install node-postgres package

<InstallPackages lib='pg' devlib=' @types/pg'/>

Step 2 - Setup connection variables

Create a .env file in the root of your project and add your database connection variable:

plaintext
DATABASE_URL=postgresql://{username}:{password}@{host}:{port}/postgres?sslmode=verify-full
<Callout title='tips'> You can obtain your connection credentials from the PlanetScale dashboard by navigating to your database, clicking **"Connect"**, and creating a **"Default role"**. See the [PlanetScale connection guide](https://planetscale.com/docs/postgres/tutorials/planetscale-postgres-drizzle#create-credentials-and-connect) for detailed steps. </Callout>

Step 3 - Connect Drizzle ORM to the database

<ConnectPlanetScalePostgres/>

Step 4 - Create a table

<CreateTable />

Step 5 - Setup Drizzle config file

<SetupConfig dialect='postgresql' env_variable='DATABASE_URL'/>

Step 6 - Applying changes to the database

<ApplyChanges />

Step 7 - Seed and Query the database

<QueryDatabase dialect='node-postgres' env_variable='DATABASE_URL'/>

Step 8 - Run index.ts file

<RunFile/>