Continuation of https://github.com/payloadcms/payload/pull/6245. This PR allows you to pass `blocksAsJSON: true` to SQL adapters and the adapter instead of aligning with the SQL preferred relation approach for blocks will just use a simple JSON column, which can improve performance with a large amount of blocks. To try these changes you can install `3.43.0-internal.c5bbc84`.
317 lines
13 KiB
Plaintext
317 lines
13 KiB
Plaintext
---
|
|
title: Postgres
|
|
label: Postgres
|
|
order: 50
|
|
desc: Payload supports Postgres through an officially supported Drizzle Database Adapter.
|
|
keywords: Postgres, documentation, typescript, Content Management System, cms, headless, javascript, node, react, nextjs
|
|
---
|
|
|
|
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`:
|
|
|
|
```ts
|
|
import { postgresAdapter } from '@payloadcms/db-postgres'
|
|
|
|
export default buildConfig({
|
|
// Configure the Postgres adapter here
|
|
db: postgresAdapter({
|
|
// Postgres-specific arguments go here.
|
|
// `pool` is required.
|
|
pool: {
|
|
connectionString: process.env.DATABASE_URI,
|
|
},
|
|
}),
|
|
})
|
|
```
|
|
|
|
`@payloadcms/db-vercel-postgres`:
|
|
|
|
```ts
|
|
import { vercelPostgresAdapter } from '@payloadcms/db-vercel-postgres'
|
|
|
|
export default buildConfig({
|
|
// Automatically uses process.env.POSTGRES_URL if no options are provided.
|
|
db: vercelPostgresAdapter(),
|
|
// Optionally, can accept the same options as the @vercel/postgres package.
|
|
db: vercelPostgresAdapter({
|
|
pool: {
|
|
connectionString: process.env.DATABASE_URL,
|
|
},
|
|
}),
|
|
})
|
|
```
|
|
|
|
<Banner type="info">
|
|
**Note:** If you're using `vercelPostgresAdapter` your
|
|
`process.env.POSTGRES_URL` or `pool.connectionString` points to a local
|
|
database (e.g hostname has `localhost` or `127.0.0.1`) we use the `pg` module
|
|
for pooling instead of `@vercel/postgres`. This is because `@vercel/postgres`
|
|
doesn't work with local databases, if you want to disable that behavior, you
|
|
can pass `forceUseVercelPostgres: true` to the adapter's args and follow
|
|
[Vercel
|
|
guide](https://vercel.com/docs/storage/vercel-postgres/local-development#option-2:-local-postgres-instance-with-docker)
|
|
for a Docker Neon DB setup.
|
|
</Banner>
|
|
|
|
## Options
|
|
|
|
| Option | Description |
|
|
| --------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
| `pool` \* | [Pool connection options](https://orm.drizzle.team/docs/quick-postgresql/node-postgres) that will be passed to Drizzle and `node-postgres` or to `@vercel/postgres` |
|
|
| `push` | Disable Drizzle's [`db push`](https://orm.drizzle.team/kit-docs/overview#prototyping-with-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](https://orm.drizzle.team/docs/transactions) |
|
|
| `disableCreateDatabase` | Pass `true` to disable 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](#beforeschemainit) |
|
|
| `afterSchemaInit` | Drizzle schema hook. Runs after the schema is built. [More Details](#afterschemainit) |
|
|
| `generateSchemaOutputFile` | Override generated schema from `payload generate:db-schema` file path. Defaults to `{CWD}/src/payload-generated.schema.ts` |
|
|
| `allowIDOnCreate` | Set to `true` to use the `id` passed in data on the create API operations without using a custom ID field. |
|
|
| `readReplicas` | An array of DB read replicas connection strings, can be used to offload read-heavy traffic. |
|
|
| `blocksAsJSON` | Store blocks as a JSON column instead of using the relational structure which can improve performance with a large amount of blocks |
|
|
|
|
## Access to Drizzle
|
|
|
|
After Payload is initialized, this adapter will expose the full power of Drizzle to you for use if you need it.
|
|
|
|
To ensure type-safety, you need to generate Drizzle schema first with:
|
|
|
|
```sh
|
|
npx payload generate:db-schema
|
|
```
|
|
|
|
Then, you can access Drizzle as follows:
|
|
|
|
```ts
|
|
import { posts } from './payload-generated-schema'
|
|
// To avoid installing Drizzle, you can import everything that drizzle has from our re-export path.
|
|
import { eq, sql, and } from '@payloadcms/db-postgres/drizzle'
|
|
|
|
// Drizzle's Querying API: https://orm.drizzle.team/docs/rqb
|
|
const posts = await payload.db.drizzle.query.posts.findMany()
|
|
// Drizzle's Select API https://orm.drizzle.team/docs/select
|
|
const result = await payload.db.drizzle
|
|
.select()
|
|
.from(posts)
|
|
.where(
|
|
and(eq(posts.id, 50), sql`lower(${posts.title}) = 'example post title'`),
|
|
)
|
|
```
|
|
|
|
## 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`](https://orm.drizzle.team/kit-docs/overview#prototyping-with-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`](/docs/database/migrations) 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](./migrations#when-to-run-migrations).
|
|
|
|
## 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.
|
|
|
|
```ts
|
|
import { postgresAdapter } from '@payloadcms/db-postgres'
|
|
import {
|
|
integer,
|
|
pgTable,
|
|
serial,
|
|
} from '@payloadcms/db-postgres/drizzle/pg-core'
|
|
|
|
postgresAdapter({
|
|
beforeSchemaInit: [
|
|
({ schema, adapter }) => {
|
|
return {
|
|
...schema,
|
|
tables: {
|
|
...schema.tables,
|
|
addedTable: pgTable('added_table', {
|
|
id: serial('id').notNull(),
|
|
}),
|
|
},
|
|
}
|
|
},
|
|
],
|
|
})
|
|
```
|
|
|
|
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](https://orm.drizzle.team/kit-docs/commands#introspect--pull)
|
|
You should get the `schema.ts` file which may look like this:
|
|
|
|
```ts
|
|
import {
|
|
pgTable,
|
|
uniqueIndex,
|
|
serial,
|
|
varchar,
|
|
text,
|
|
} from 'drizzle-orm/pg-core'
|
|
|
|
export const users = pgTable('users', {
|
|
id: serial('id').primaryKey(),
|
|
fullName: text('full_name'),
|
|
phone: varchar('phone', { length: 256 }),
|
|
})
|
|
|
|
export const countries = pgTable(
|
|
'countries',
|
|
{
|
|
id: serial('id').primaryKey(),
|
|
name: varchar('name', { length: 256 }),
|
|
},
|
|
(countries) => {
|
|
return {
|
|
nameIndex: uniqueIndex('name_idx').on(countries.name),
|
|
}
|
|
},
|
|
)
|
|
```
|
|
|
|
You can import them into your config and append to the schema with the `beforeSchemaInit` hook like this:
|
|
|
|
```ts
|
|
import { postgresAdapter } from '@payloadcms/db-postgres'
|
|
import { users, countries } from '../drizzle/schema'
|
|
|
|
postgresAdapter({
|
|
beforeSchemaInit: [
|
|
({ schema, adapter }) => {
|
|
return {
|
|
...schema,
|
|
tables: {
|
|
...schema.tables,
|
|
users,
|
|
countries,
|
|
},
|
|
}
|
|
},
|
|
],
|
|
})
|
|
```
|
|
|
|
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` utility to the args. You can refer to the [Drizzle documentation](https://orm.drizzle.team/docs/sql-schema-declaration).
|
|
The following example adds the `extra_integer_column` column and a composite index on `country` and `city` columns.
|
|
|
|
```ts
|
|
import { postgresAdapter } from '@payloadcms/db-postgres'
|
|
import { index, integer } from '@payloadcms/db-postgres/drizzle/pg-core'
|
|
import { buildConfig } from 'payload'
|
|
|
|
export default buildConfig({
|
|
collections: [
|
|
{
|
|
slug: 'places',
|
|
fields: [
|
|
{
|
|
name: 'country',
|
|
type: 'text',
|
|
},
|
|
{
|
|
name: 'city',
|
|
type: 'text',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
db: postgresAdapter({
|
|
afterSchemaInit: [
|
|
({ schema, extendTable, adapter }) => {
|
|
extendTable({
|
|
table: schema.tables.places,
|
|
columns: {
|
|
extraIntegerColumn: integer('extra_integer_column'),
|
|
},
|
|
extraConfig: (table) => ({
|
|
country_city_composite_index: index(
|
|
'country_city_composite_index',
|
|
).on(table.country, table.city),
|
|
}),
|
|
})
|
|
|
|
return schema
|
|
},
|
|
],
|
|
}),
|
|
})
|
|
```
|
|
|
|
### Note for generated schema:
|
|
|
|
Columns and tables, added in schema hooks won't be added to the generated via `payload generate:db-schema` Drizzle schema.
|
|
If you want them to be there, you either have to edit this file manually or mutate the internal Payload "raw" SQL schema in the `beforeSchemaInit`:
|
|
|
|
```ts
|
|
import { postgresAdapter } from '@payloadcms/db-postgres'
|
|
|
|
postgresAdapter({
|
|
beforeSchemaInit: [
|
|
({ schema, adapter }) => {
|
|
// Add a new table
|
|
adapter.rawTables.myTable = {
|
|
name: 'my_table',
|
|
columns: {
|
|
my_id: {
|
|
name: 'my_id',
|
|
type: 'serial',
|
|
primaryKey: true,
|
|
},
|
|
},
|
|
}
|
|
|
|
// Add a new column to generated by Payload table:
|
|
adapter.rawTables.posts.columns.customColumn = {
|
|
name: 'custom_column',
|
|
// Note that Payload SQL doesn't support everything that Drizzle does.
|
|
type: 'integer',
|
|
notNull: true,
|
|
}
|
|
// Add a new index to generated by Payload table:
|
|
adapter.rawTables.posts.indexes.customColumnIdx = {
|
|
name: 'custom_column_idx',
|
|
unique: true,
|
|
on: ['custom_column'],
|
|
}
|
|
|
|
return schema
|
|
},
|
|
],
|
|
})
|
|
```
|