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.
Alternatively, the @payloadcms/db-vercel-postgres
package is also available and is optimized for use with Vercel.
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:
Usage
@payloadcms/db-postgres
:
@payloadcms/db-vercel-postgres
:
Options
Option | Description |
---|---|
pool * | Pool connection options that will be passed to Drizzle and node-postgres or to @vercel/postgres |
push | Disable Drizzle's db push in development mode. By default, push is enabled for development mode only. |
migrationDir | Customize the directory that migrations are stored. |
schemaName (experimental) | A string for the postgres schema to use, defaults to 'public'. |
idType | A string of 'serial', or 'uuid' that is used for the data type given to id columns. |
transactionOptions | A PgTransactionConfig object for transactions, or set to false to disable using transactions. More details |
disableCreateDatabase | Pass true to disale auto database creation if it doesn't exist. Defaults to false . |
localesSuffix | A string appended to the end of table names for storing localized fields. Default is '_locales'. |
relationshipsSuffix | A string appended to the end of table names for storing relationships. Default is '_rels'. |
versionsSuffix | A string appended to the end of table names for storing versions. Defaults to '_v'. |
beforeSchemaInit | Drizzle schema hook. Runs before the schema is built. More Details |
afterSchemaInit | Drizzle schema hook. Runs after the schema is built. More Details |
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:
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.
Drizzle schema hooks
beforeSchemaInit
Runs before the schema is built. You can use this hook to extend your database structure with tables that won't be managed by Payload.
One use case is preserving your existing database structure when migrating to Payload. By default, Payload drops the current database schema, which may not be desirable in this scenario.
To quickly generate the Drizzle schema from your database you can use Drizzle Introspection
You should get the schema.ts
file which may look like this:
You can import them into your config and append to the schema with the beforeSchemaInit
hook like this:
Make sure Payload doesn't overlap table names with its collections. For example, if you already have a collection with slug "users", you should either change the slug or dbName
to change the table name for this collection.
afterSchemaInit
Runs after the Drizzle schema is built. You can use this hook to modify the schema with features that aren't supported by Payload, or if you want to add a column that you don't want to be in the Payload config.
To extend a table, Payload exposes extendTable
utillity to the args. You can refer to the Drizzle documentation.
The following example adds the extra_integer_column
column and a composite index on country
and city
columns.