From ae5a1a9af23665e5989524153a15e7804ef09b5a Mon Sep 17 00:00:00 2001 From: Tarek Al-Qarqaz Date: Sat, 30 Mar 2024 19:38:23 +0000 Subject: [PATCH] feat: PostgresProvider - Add POSTGRES_URL override. --- .../installation/backend-config.md | 1 + .../getting-started/installation/postgres.md | 55 +++++++++++-------- mealie/core/settings/db_providers.py | 11 +++- 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/docs/docs/documentation/getting-started/installation/backend-config.md b/docs/docs/documentation/getting-started/installation/backend-config.md index 8f0379c2f8f2..83a14d3d06cf 100644 --- a/docs/docs/documentation/getting-started/installation/backend-config.md +++ b/docs/docs/documentation/getting-started/installation/backend-config.md @@ -33,6 +33,7 @@ | POSTGRES_SERVER | postgres | Postgres database server address | | POSTGRES_PORT | 5432 | Postgres database port | | POSTGRES_DB | mealie | Postgres database name | +| POSTGRES_URL | None | Postgres URL override. Must be used with DB_ENGINE: postgres | ### Email diff --git a/docs/docs/documentation/getting-started/installation/postgres.md b/docs/docs/documentation/getting-started/installation/postgres.md index 7e0f33a4277d..9ffc94ac4c8c 100644 --- a/docs/docs/documentation/getting-started/installation/postgres.md +++ b/docs/docs/documentation/getting-started/installation/postgres.md @@ -5,40 +5,41 @@ PostgreSQL might be considered if you need to support many concurrent users. In **For Environment Variable Configuration, see** [Backend Configuration](./backend-config.md) ```yaml ---- -version: "3.7" services: mealie: - image: ghcr.io/mealie-recipes/mealie:v1.3.2 # (3) + image: ghcr.io/mealie-recipes/mealie:1.3.2 # (3) container_name: mealie + restart: always ports: - "9925:9000" # (1) deploy: resources: limits: memory: 1000M # (2) - depends_on: - - postgres volumes: - mealie-data:/app/data/ + - /etc/timezone:/etc/timezone:ro environment: - # Set Backend ENV Variables Here - - ALLOW_SIGNUP=true - - PUID=1000 - - PGID=1000 - - TZ=America/Anchorage - - MAX_WORKERS=1 - - WEB_CONCURRENCY=1 - - BASE_URL=https://mealie.yourdomain.com + # Set Backend ENV Variables Here + ALLOW_SIGNUP: true + PUID: 1000 + PGID: 1000 + MAX_WORKERS: 1 + WEB_CONCURRENCY: 1 + BASE_URL: http://localhost # (4) + # Database Settings + DB_ENGINE: postgres + POSTGRES_URL: 'postgres://mealie:mealie@postgres/mealie' # (5) + # The following variables are supported if you do not specify POSTGRES_URL + # POSTGRES_USER: mealie + # POSTGRES_PASSWORD: mealie + # POSTGRES_SERVER: postgres + # POSTGRES_PORT: 5432 + # POSTGRES_DB: mealie + depends_on: + postgres: + condition: service_healthy - # Database Settings - - DB_ENGINE=postgres - - POSTGRES_USER=mealie - - POSTGRES_PASSWORD=mealie - - POSTGRES_SERVER=postgres - - POSTGRES_PORT=5432 - - POSTGRES_DB=mealie - restart: always postgres: container_name: postgres image: postgres:15 @@ -48,12 +49,15 @@ services: environment: POSTGRES_PASSWORD: mealie POSTGRES_USER: mealie + healthcheck: + test: ["CMD", "pg_isready"] + interval: 30s + timeout: 20s + retries: 3 volumes: mealie-data: - driver: local mealie-pgdata: - driver: local ``` @@ -61,3 +65,8 @@ volumes: 1. To access the mealie interface you only need to expose port 9000 on the mealie container. Here we expose port 9925 on the host, but feel free to change this to any port you like. 2. Setting an explicit memory limit is recommended. Python can pre-allocate larger amounts of memory than is necessary if you have a machine with a lot of RAM. This can cause the container to idle at a high memory usage. Setting a memory limit will improve idle performance. 3. You should double check this value isn't out of date when setting up for the first time; check the README and use the value from the "latest release" badge at the top - the format should be `vX.Y.Z`. Whilst a 'latest' tag is available, the Mealie team advises specifying a specific version tag and consciously updating to newer versions when you have time to read the release notes and ensure you follow any manual actions required (which should be rare). +4. You should replace this with your domain eg. mealie.yourdomain.com +5. When set, this variable will override POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_SERVER, +POSTGRES_PORT, and POSTGRES_DB. It must be used in conjunction with DB_ENGINE: postgres. +The format is 'postgresql://POSTGRES_USER:POSTGRES_PASSWORD@POSTGRES_SERVER:POSTGRES_PORT/POSTGRES_DB' +You may pass in a UNIX socket with the following format: postgresql://POSTGRES_USER:POSTGRES_PASSWORD@/POSTGRES_DB?host=/run/postgresql', where the path at the end is the parent directory of the socket. diff --git a/mealie/core/settings/db_providers.py b/mealie/core/settings/db_providers.py index 9cd1a5aea101..07d7faf07ea2 100644 --- a/mealie/core/settings/db_providers.py +++ b/mealie/core/settings/db_providers.py @@ -39,22 +39,27 @@ class PostgresProvider(AbstractDBProvider, BaseSettings): POSTGRES_SERVER: str = "postgres" POSTGRES_PORT: str = "5432" POSTGRES_DB: str = "mealie" + POSTGRES_URL: str = "" model_config = SettingsConfigDict(arbitrary_types_allowed=True, extra="allow") @property def db_url(self) -> str: - host = f"{self.POSTGRES_SERVER}:{self.POSTGRES_PORT}" - return str( + if self.POSTGRES_URL: + return self.POSTGRES_URL + + self.db_url: str = str( PostgresDsn.build( scheme="postgresql", username=self.POSTGRES_USER, password=urlparse.quote_plus(self.POSTGRES_PASSWORD), - host=host, + host=f"{self.POSTGRES_SERVER}:{self.POSTGRES_PORT}", path=f"{self.POSTGRES_DB or ''}", ) ) + return self.POSTGRES_URL + @property def db_url_public(self) -> str: user = self.POSTGRES_USER