Postgres Adapter throws `error: relation "users" does not exist` on Production Builds and Local API
Link to reproduction
No response
Describe the Bug
The Postgres Adapter errors due to missing relations (specifically users, but others error as well when queried using the Local API), either using the Local API or the built nextjs admin panel.
This only errors if next dev has never been run.
I assume running next dev seeds the postgres database so from that point on both the local api, and the admin panel are able to resolve the postgres relations. However this should be able work without seeding the database.
To Reproduce
- Clone and
cdinto payloadcms/payload-3.0-demo - Run
pnpm install - Use Postgres:
// payload.config.ts import { postgresAdapter } from '@payloadcms/db-postgres' export default buildConfig({ // ... db: postgresAdapter({ pool: { connectionString: process.env.POSTGRES_URI || '', }, }) // ... }) - Start the database:
$ ./start-database.sh - Build it:
This already errors because$ PAYLOAD_SECRET=secret POSTGRES_URI=postgresql://postgres:[email protected]:5432/next-payload-3 pnpm buildmy-routetries to query the empty postgres database:error: relation "users" does not exist - Remove
my-route:$ rm -rf src/app/my-route - Build it again:
$ pnpm build - Start it:
$ PAYLOAD_SECRET=secret POSTGRES_URI=postgresql://postgres:[email protected]:5432/next-payload-3 pnpm start - Navigate to http://localhost:3000/admin and receive the same error.
Payload Version
3.0
Adapters and Plugins
db-postgres
This is not unexpected. Here is why:
A migration must be ran on your database in order to scaffold out the necessary tables.
This happens in one of two ways:
- The
pnpm devcommand is run, which automatically pushes the new schema to the database - A migration is created with
pnpm migrate:create <optional-name>andpnpm migrate
Without one of those two, the database will never be in the proper state to recognize any collection, including users.
Makes perfect sense, thanks for the clarification. I wasn't aware that pnpm dev also did this under the hood 👍🏼
This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.