payload icon indicating copy to clipboard operation
payload copied to clipboard

Postgres Adapter throws `error: relation "users" does not exist` on Production Builds and Local API

Open emonadeo opened this issue 1 year ago • 1 comments

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

  1. Clone and cd into payloadcms/payload-3.0-demo
  2. Run pnpm install
  3. Use Postgres:
    // payload.config.ts
    import { postgresAdapter } from '@payloadcms/db-postgres'
    export default buildConfig({
      // ...
      db: postgresAdapter({
        pool: {
          connectionString: process.env.POSTGRES_URI || '',
        },
      })
      // ...
    })
    
  4. Start the database:
    $ ./start-database.sh
    
  5. Build it:
    $ PAYLOAD_SECRET=secret POSTGRES_URI=postgresql://postgres:[email protected]:5432/next-payload-3 pnpm build
    
    This already errors because my-route tries to query the empty postgres database:
    error: relation "users" does not exist
    
  6. Remove my-route:
    $ rm -rf src/app/my-route
    
  7. Build it again:
    $ pnpm build
    
  8. Start it:
    $ PAYLOAD_SECRET=secret POSTGRES_URI=postgresql://postgres:[email protected]:5432/next-payload-3 pnpm start
    
  9. Navigate to http://localhost:3000/admin and receive the same error.

Payload Version

3.0

Adapters and Plugins

db-postgres

emonadeo avatar May 06 '24 23:05 emonadeo

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 dev command is run, which automatically pushes the new schema to the database
  • A migration is created with pnpm migrate:create <optional-name> and pnpm migrate

Without one of those two, the database will never be in the proper state to recognize any collection, including users.

denolfe avatar May 07 '24 00:05 denolfe

Makes perfect sense, thanks for the clarification. I wasn't aware that pnpm dev also did this under the hood 👍🏼

emonadeo avatar May 14 '24 01:05 emonadeo

This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.

github-actions[bot] avatar Sep 07 '24 04:09 github-actions[bot]