Postgres

To use Payload with Postgres, install the package @payloadcms/db-postgres. It leverages Drizzle ORM and node-postgres to interact with a Postgres database that you provide.

It automatically manages changes to your database for you in development mode, and exposes a full suite of migration controls for you to leverage in order to keep other database environments in sync with your schema. DDL transformations are automatically generated.

To configure Payload to use Postgres, pass the postgresAdapter to your Payload Config as follows:

1
import { postgresAdapter } from '@payloadcms/db-postgres'
2
3
export default buildConfig({
4
// Your config goes here
5
collections: [
6
// Collections go here
7
],
8
// Configure the Postgres adapter here
9
db: postgresAdapter({
10
// Postgres-specific arguments go here.
11
// `pool` is required.
12
pool: {
13
connectionString: process.env.DATABASE_URI,
14
},
15
}),
16
})

Options

OptionDescription
pool *Pool connection options that will be passed to Drizzle and node-postgres.
pushDisable Drizzle's db push in development mode. By default, push is enabled for development mode only.
migrationDirCustomize the directory that migrations are stored.
loggerThe instance of the logger to be passed to drizzle. By default Payload's will be used.
schemaNameA string for the postgres schema to use, defaults to 'public'.
localesSuffixA string appended to the end of table names for storing localized fields. Default is '_locales'.
relationshipsSuffixA string appended to the end of table names for storing relationships. Default is '_rels'.
versionsSuffixA string appended to the end of table names for storing versions. Defaults to '_v'.

Access to Drizzle

After Payload is initialized, this adapter will expose the full power of Drizzle to you for use if you need it.

You can access Drizzle as follows:

1
payload.db.drizzle

Tables, relations, and enums

In addition to exposing Drizzle directly, all of the tables, Drizzle relations, and enum configs are exposed for you via the payload.db property as well.

  • Tables - payload.db.tables
  • Enums - payload.db.enums
  • Relations - payload.db.relations

Prototyping in development mode

Drizzle exposes two ways to work locally in development mode.

The first is db push, which automatically pushes changes you make to your Payload Config (and therefore, Drizzle schema) to your database so you don't have to manually migrate every time you change your Payload Config. This only works in development mode, and should not be mixed with manually running migrate commands.

You will be warned if any changes that you make will entail data loss while in development mode. Push is enabled by default, but you can opt out if you'd like.

Alternatively, you can disable push and rely solely on migrations to keep your local database in sync with your Payload Config.

Migration workflows

In Postgres, migrations are a fundamental aspect of working with Payload and you should become familiar with how they work.

For more information about migrations, click here.

Next

Fields Overview