Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Initial setup doesn't work with PostgreSQL & migrations in production?

default discord avatar
.nobeylast year
6

I've seen a few other posts (

https://discord.com/channels/967097582721572934/967097582721572937/1174318822744936488

) that don't have an actual solution, but I'm also not able to get a production version of Payload running in a docker container using the Postgres adapter. It appears an initialization that is supposed to happen with Payload doesn't happen. I get an error on the server

ERROR (payload): error: relation "users" does not exist

when visiting the

/admin

route on initial setup.



I have a Dockerfile that can build and run the app just fine:



FROM node:21 as base

RUN mkdir -p /home/node/app && chown -R node:node /home/node/app

WORKDIR /home/node/app

FROM base as build

COPY --chown=node:node package.json package-lock.json /home/node/app/
COPY --chown=node:node tsconfig.json /home/node/app/
COPY --chown=node:node src /home/node/app/src

RUN npm install
RUN npm run build

FROM base as runtime

ENV NODE_ENV=production
ENV PAYLOAD_CONFIG_PATH=dist/payload.config.js

COPY --chown=node:node package.json package-lock.json /home/node/app/
RUN npm ci --omit=dev

COPY --chown=node:node --from=build /home/node/app/dist /home/node/app/dist
COPY --chown=node:node --from=build /home/node/app/build /home/node/app/build

USER node

CMD npx payload migrate && node dist/server.js


npx payload migrate

is pointing at the

src/migrations

directory though, which doesn't actually exist in the

runtime

docker image result. Are these migrations just any custom migrations I write or should I expect payload migrations to be there too? This leads me to my last question: how are migrations supposed to be ran in production when I don't include the

src

directory in my final build? Here is the error for reference:



ERROR (payload): No migration directory found at /home/node/app/src/migrations

Thanks for any help!



Noah J. (SPARK Business Works)



@jmikrut - you mentioned I could ping you if I had a question on Payload and I've now done some homework!



I don't have Discord Nitro so I'm limited on my post size 🙄



Here are a few more links:


*

https://discord.com/channels/967097582721572934/1166861750537310278/1166873848411607110

*

https://discord.com/channels/967097582721572934/1164824997265670194/1164824997265670194
  • default discord avatar
    pedogunulast year

    You have to specify migration directory.


    https://payloadcms.com/docs/database/migrations#migration-file-contents
    Payload stores all created migrations in a folder that you can specify. By default, migrations are stored in ./src/migrations.

    There are

    migrationDir

    option in

    mongooseAdapter

    and

    postgresAdapter

    .

  • default discord avatar
    .nobeylast year

    Cool, so that answers my second question. I don't actually have any migrations yet, it was just for future reference. That doc also mentions that


    Payload will check the default and possibly make a best effort to find your migrations directory by searching in common locations ie. ./src/migrations, ./dist/migrations, ./migrations, etc.

    but I guess it makes sense to be explicit for my app! Thanks!

  • discord user avatar
    jmikrut
    last year

    hey @nobey - good to see you here!



    how are migrations supposed to be ran in production when I don't include the src directory in my final build?


    that's really up to you and your build process - i.e. if you are only including your

    /dist

    folder, then you can run them from

    /dist


    it doesn't really matter if you run them from

    /src

    or

    /dist
  • default discord avatar
    .nobeylast year

    That part makes sense. Do I need to write migrations for each of the collections & fields I create then to get this up and running in production with Postgres? Locally, I can just create the different schemas and the collections just show up in the Admin panel without me having to do anything, but this isn't the case in production.

  • discord user avatar
    jmikrut
    last year

    I am actually going to do a video on this



    but the TL;DR



    1. use push mode in dev, get everything to a spot where you're happy with it (feature completed, etc)


    2. then create a migration


    3. then ONLY run the migration against your non-dev DBs before you deploy your new code

  • default discord avatar
    .nobeylast year

    Got it, I was assuming Payload would handle and apply schema changes under the hood. This might prompt some more questions down the line once I use it a bit more. There wouldn't happen to be a payload command to generate migrations based on the current schema, would there? 😅 Maybe a roadmap item 😉



    Thanks for the help!!



    Never mind, this is already a thing!

    npm run payload migrate:create

    -- cool! Thanks again 🙂

    https://payloadcms.com/docs/database/postgres#migration-workflows
Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.