Skip to content

Commit

Permalink
set up a postgres container properly
Browse files Browse the repository at this point in the history
I just learned this is how you're actually supposed to do it in order to
mimic a real deployment as closely as possible. If you read the
documentation for this docker image, you'll find that the user created
via `POSTGRES_USER` has `SUPERUSER` rights. Generally, real deployments
don't and shouldn't have this set for the database user of random
services (such as Lemmy).

The proper way to do this is to use the defaults and add an init script
that adds the actual service user and database to this specific
directory. This results in the Lemmy service user not having
`SUPERUSER`, which ensures that future migrations aren't written with
the worrying assumption that people will grant it `SUPERUSER` rights. It
also breaks the existing migrations because this exact mistake has been
made already.

I did this by adding a new `docker-compose.yml` to the repo root for two
reasons:

1. It's more convenient to invoke when located here
2. I don't really know what's going on in `docker/docker-compose.yml`
   and what all it's used for, so I opted not to mess with it

Obviously this is suboptimal, but it gets the point across. Please point
out to me the proper way to make this change and I'll amend this commit.
  • Loading branch information
CobaltCause committed Jun 10, 2023
1 parent 9d6a9f1 commit 2c2413e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
postgres:
image: postgres:15-alpine
environment:
- POSTGRES_PASSWORD=postgres
volumes:
- ./docker/init-user-db.sh:/docker-entrypoint-initdb.d/init-user-db.sh
ports:
- 5432:5432
7 changes: 7 additions & 0 deletions docker/init-user-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

psql -v ON_ERROR_STOP=1 -U "$POSTGRES_USER" -d "$POSTGRES_DB" <<-ESQL
CREATE USER lemmy WITH PASSWORD 'password';
CREATE DATABASE lemmy WITH OWNER lemmy;
GRANT ALL PRIVILEGES ON DATABASE lemmy TO lemmy;
ESQL

0 comments on commit 2c2413e

Please sign in to comment.