Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
emonadeo opened this issue May 6, 2024 · 2 comments
Assignees
Labels
db-postgres @payloadcms/db-postgres [possible-bug] Possible bug which hasn't been reproduced yet

Comments

@emonadeo
Copy link

emonadeo commented May 6, 2024

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:password@127.0.0.1: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:password@127.0.0.1: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

@denolfe
Copy link
Member

denolfe commented May 7, 2024

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 denolfe self-assigned this May 7, 2024
@AlessioGr AlessioGr added the db-postgres @payloadcms/db-postgres label May 9, 2024
@emonadeo
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
db-postgres @payloadcms/db-postgres [possible-bug] Possible bug which hasn't been reproduced yet
Projects
None yet
Development

No branches or pull requests

3 participants