fix(helm): add pgdata init container and PGDATA env for PostgreSQL StatefulSet#464
fix(helm): add pgdata init container and PGDATA env for PostgreSQL StatefulSet#464nethi wants to merge 2 commits intojonwiggins:mainfrom
Conversation
…atefulSet The PostgreSQL pod failed to start with permission errors when TLS was enabled because /var/lib/postgresql/data/pgdata did not exist before the main container ran. Adds a busybox init container to pre-create and chmod the directory, and sets PGDATA explicitly so PostgreSQL uses the subdirectory rather than the mount root. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Thanks for this — the intent (standard PGDATA-as-subdirectory pattern) is right, but two issues before this can merge: 1. 2. Options:
The |
|
Good points. will update PR with the suggested fixes |
Addresses PR jonwiggins#464 feedback: - Issue jonwiggins#1: init containers no longer inside TLS conditional - Issue jonwiggins#2: PGDATA change now opt-in via usePgdataSubdirectory toggle - Added migration documentation in NOTES.txt and CHANGELOG - Bumped chart version 0.1.0 → 0.1.1 Root Cause Fix (Docker Desktop & other platforms): The PostgreSQL container with capabilities.drop=ALL cannot chmod its data directory on Docker Desktop and some K8s distros. Fixed by adding init-pgdata container running as root to pre-fix permissions before PostgreSQL starts. Changes: 1. values.yaml: - Add postgresql.usePgdataSubdirectory (default: false) - Backward compatible - existing deployments preserved 2. postgres.yaml: - Add init-pgdata container (always runs as root) - Fixes ownership/permissions: chown 999:999 + chmod 700 - Conditionally creates /var/lib/postgresql/data/pgdata subdirectory - Combines permission fix + subdirectory in single container - Make PGDATA env conditional (only set when subdirectory enabled) - Add fsGroupChangePolicy: OnRootMismatch (performance optimization) 3. NOTES.txt: - Show migration warning when subdirectory disabled - Explain PostgreSQL 18+ requirements - Reference CHANGELOG for migration steps 4. Chart.yaml: - Bump version: 0.1.0 → 0.1.1 - Update appVersion: 0.1.0 → 0.3.1 5. CHANGELOG.md: - Document new feature in [Unreleased] section - Note breaking change warning Init Container Strategy: - init-pgdata: Always runs, fixes permissions + optional subdirectory - init-tls: Only when postgresql.tls.enabled=true PostgreSQL Version Support: - PostgreSQL 16: Works with or without subdirectory - PostgreSQL 18: Requires usePgdataSubdirectory=true (Docker image requirement) Tested Scenarios: ✓ PostgreSQL 16 + subdirectory disabled + TLS enabled ✓ PostgreSQL 16 + subdirectory enabled + TLS enabled ✓ PostgreSQL 18 + subdirectory enabled + TLS enabled ✓ Migration warning displays correctly in NOTES.txt ✓ Fresh deployments on Docker Desktop ✓ Helm templating inside shell args verified Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
Thanks for tackling this — the flag-gated approach is the right shape. A few things to address before merge: Issues1. PR description contradicts the code securityContext:
runAsUser: 0
runAsNonRoot: falseNo 2.
Suggest gating the container behind 3. 4. "PostgreSQL 18 requirement" framing is misleading 5. Chart versioning 6. No upgrade-path test evidence Smaller notes
Suggested minimal fixes
|
|
Thanks @jonwiggins will address the issues. I was originally trying to tackle the above issue as I ran into postgres in crashloop with fresh install. Now that that is fixed, may be I should just convert this to "enabling/supporting postgres 18", assuming that there is still an issue. |
|
@nethi — circling back on this. Looks like #468 (the original crashloop motivator) is already fixed in b94c666, and you mentioned retargeting this as PG18 enablement instead. That sounds like the right call — drop the unconditional Want to push that pivot when you have a chance? Happy to merge once it's reframed; it's been sitting for a couple weeks and I don't want it to bitrot. |
Summary
Fixes PostgreSQL pod startup failures when TLS is enabled. When using K8s in Docker desktop on MacOS, postgres pod failed with permission errors because the /var/lib/postgresql/data/pgdata subdirectory did not exist before PostgreSQL initialization.
PostgreSQL 18 Requirement: PostgreSQL 18+ enforces stricter requirements for the data directory structure. When using persistent volumes, PostgreSQL expects PGDATA to point to a subdirectory (not the mount root) to avoid conflicts with volume metadata files like lost+found. Without this, PostgreSQL initialization fails.
Changes
Testing
pnpm turbo test)pnpm turbo typecheck)Verified PostgreSQL pod starts successfully with TLS enabled in both fresh deployments and upgrades.
Related
Resolves pod crash loop when deploying with postgresql.tls.enabled=true. Required for PostgreSQL 18 compatibility and persistent volume best practices.