Skip to content

feat(postgres): Support postgres:18, fixes #7661 - #7662

Merged
rfay merged 12 commits into
ddev:mainfrom
rfay:20250925_rfay_postgres_18
Sep 30, 2025
Merged

rfay merged 12 commits into
ddev:mainfrom
rfay:20250925_rfay_postgres_18

Conversation

@rfay

@rfay rfay commented Sep 25, 2025

Copy link
Copy Markdown
Member

The Issue

Postgres:18 is out today, https://www.postgresql.org/about/news/postgresql-18-released-3142/

It breaks our builds because we get the wrong client (TestWebserverPostgresDBClient )

https://github.com/ddev/ddev/actions/runs/18019471809/job/51272939982#step:17:8534

How This PR Solves The Issue

It's not quite ready yet, but should work when there's a docker image.

Manual Testing Instructions

Automated Testing Overview

Release/Deployment Notes

@rfay
rfay force-pushed the 20250925_rfay_postgres_18 branch from 006cdc3 to 99b8a5b Compare September 26, 2025 03:05
@rfay

rfay commented Sep 26, 2025

Copy link
Copy Markdown
Member Author

postgres:18 is out now, but doesn't work. I think it's because the data directory has a data symlink in it, broken.
Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error mounting "/var/lib/docker/volumes/d11-postgres-postgres/_data" to rootfs at "/var/lib/postgresql/data": change mount propagation through procfd: open o_path procfd: open /var/lib/docker/overlay2/0d1565aaa1741b5667f125184c44e3a08895ce93dc6fbcd121e8d3ec858c2266/merged/var/lib/postgresql/data: no such file or directory'

@stasadev

stasadev commented Sep 26, 2025 •

Copy link
Copy Markdown
Member

Should be good now.

@stasadev stasadev changed the title feat(postgres): Support postgres:18, fixes #7661 [skip ci] feat(postgres): Support postgres:18, fixes #7661 Sep 26, 2025
@github-actions

github-actions Bot commented Sep 26, 2025 •

Copy link
Copy Markdown

@stasadev

Copy link
Copy Markdown
Member

Requires rebase after #7665 goes in.

@rfay rfay left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for sorting this out!

Comment thread pkg/ddevapp/config.go Outdated
templateVars.DBMountDir = "/var/lib/postgresql/data"
} else {
// Postgres 18+ changed the default mount point
// See https://github.com/docker-library/postgres/pull/1259

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, thank you for figuring it out. Is it possible that we could use PGDATA instead of this? check? If not, the logic might be easier to look at if reversed, but I'm sure you already thought about that. (dbmountdir=/var/lib/postgresql; if ver < 18 dbmountdir=/var/lib/postgresql/data)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think PGDATA can change anything here. The change is in the volume (and we can't change this):

VOLUME /var/lib/postgresql

Previously, it was:

VOLUME /var/lib/postgresql/data

the logic might be easier to look at if reversed

Yes, but in this case, we need to convert app.Database.Version to a number.
That leads to using a magic number in the code.

It would be shorter, but using constants is better.

@rfay
rfay force-pushed the 20250925_rfay_postgres_18 branch from 121f520 to 4f954b4 Compare September 26, 2025 13:43
@rfay
rfay marked this pull request as ready for review September 26, 2025 13:43
@rfay
rfay requested review from a team as code owners September 26, 2025 13:43
@rfay

rfay commented Sep 26, 2025

Copy link
Copy Markdown
Member Author

Rebased

stasadev

This comment was marked as outdated.

@stasadev
stasadev self-requested a review September 26, 2025 15:35
rfay and others added 4 commits September 26, 2025 10:34
PostgreSQL 18 changed the directory structure from `/var/lib/postgresql/data`
to `/var/lib/postgresql/[version]/docker`, requiring updates to the database
type detection logic.

## Changes

- Refactored `GetExistingDBType()` to be more readable and maintainable
- Extracted database version detection logic to `getDBVersionFromVolume()`
- Added support for PostgreSQL 18+ directory structure detection
- Maintains backward compatibility with pre-18 PostgreSQL versions
- Added comprehensive unit tests for all database scenarios

## Testing

- Tests cover MySQL/MariaDB, PostgreSQL 17 (pre-18), PostgreSQL 18+ (new structure)
- Shell script logic is directly tested using Docker containers
- All existing tests continue to pass

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Generalizes PostgreSQL 18+ directory path handling across the DDEV codebase
by creating a GetPostgresDataDir() helper function that returns the correct
data directory path based on PostgreSQL version.

Changes:
- Add GetPostgresDataDir() helper in db.go that returns:
  - /var/lib/postgresql/data for PostgreSQL ≤17
  - /var/lib/postgresql for PostgreSQL 18+
- Update snapshot restore to use GetPostgresDataDir() instead of hardcoded paths
- Update PostgreSQL volume chown operation to use GetPostgresDataDir()
- Update default working directory mapping to use GetPostgresDataDir()
- Simplify Docker Compose template generation to use GetPostgresDataDir()
- Add comprehensive tests for GetPostgresDataDir() helper function

This ensures consistent PostgreSQL directory handling across database type
detection, snapshot/restore operations, volume management, and container
configuration while maintaining full backward compatibility.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
…PostgreSQL 18 compatibility

Fixes the final hardcoded PostgreSQL paths that were preventing PostgreSQL 18
compatibility in snapshot restore operations:

- Update PostgreSQL Dockerfile template to use {{ .DBMountDir }} template variable
  instead of hardcoded /var/lib/postgresql path
- Fix recovery.conf path in snapshot restore command to use dynamic postgresDataDir
  instead of hardcoded /var/lib/postgresql/recovery.conf

This resolves the "cat: /var/lib/postgresql/recovery.conf: No such file or directory"
error that occurred during PostgreSQL 18 snapshot restore operations.

All PostgreSQL paths now correctly use the GetPostgresDataDir() helper function
across database detection, snapshot/restore, volume management, container configuration,
and Dockerfile templates, ensuring full PostgreSQL 18+ compatibility.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
…ixes snapshot/restore compatibility

PostgreSQL 18 changed the default mount point from /var/lib/postgresql/data to /var/lib/postgresql
with actual database files stored in /var/lib/postgresql/18/docker/.

This change:
- Adds GetPostgresDataDir() for mount point logic (compatible with both ≤17 and 18+)
- Adds GetPostgresDataPath() for actual data file location (version-specific for 18+)
- Updates snapshot creation to preserve PostgreSQL 18+ directory structure using mkdir/cp
- Fixes snapshot restore to use correct paths and recovery configuration
- Updates template system to use DBDataPath variable for PostgreSQL data file paths
- Adds comprehensive tests for both helper functions and PostgreSQL 18+ compatibility
- Ensures recovery.signal is placed in correct location for PostgreSQL 18+
- Adds restore_command parameter for PostgreSQL 18+ archive recovery compatibility

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Comment thread pkg/ddevapp/ddevapp.go
// PostgreSQL 18+: backup from actual data path and create tar preserving directory structure
// Create the full directory structure (e.g., 18/docker/) that matches the container layout
versionDir := filepath.Base(filepath.Dir(postgresDataPath)) // Extract "18" from "/var/lib/postgresql/18/docker"
c = fmt.Sprintf("cd %s && rm -rf /var/tmp/pgbackup && pg_basebackup -c fast -D /var/tmp/pgbackup 2>/tmp/snapshot_%s.log && mkdir -p /var/tmp/pgstructure/%s/docker && cp -a /var/tmp/pgbackup/* /var/tmp/pgstructure/%s/docker/ && tar -czf %s -C /var/tmp/pgstructure/ .", postgresDataPath, path.Base(targetFile), versionDir, versionDir, targetFile)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Impossible to understand or debug, maybe we can figure out how to improve this one and its friends.

Comment thread pkg/ddevapp/snapshot.go Outdated
targetConfName = path.Join(nodeps.PostgresConfigDir, "recovery.conf")
}
restoreCmd = fmt.Sprintf(`bash -c 'chmod 700 /var/lib/postgresql/data && mkdir -p %s && rm -rf /var/lib/postgresql/data/* && tar -C /var/lib/postgresql/data -zxf /mnt/snapshots/%s && touch /var/lib/postgresql/data/recovery.signal && cat /var/lib/postgresql/recovery.conf >>%s && postgres -c config_file=%s/postgresql.conf -c hba_file=%s/pg_hba.conf'`, confdDir, snapshotFile, targetConfName, nodeps.PostgresConfigDir, nodeps.PostgresConfigDir)
restoreCmd = fmt.Sprintf(`bash -c 'chmod 700 %s && mkdir -p %s && rm -rf %s/* && tar -C %s -zxf /mnt/snapshots/%s && chmod 700 %s && touch %s/recovery.signal && postgres -c config_file=%s/postgresql.conf -c hba_file=%s/pg_hba.conf -c restore_command=true'`, postgresDataDir, confdDir, postgresDataDir, postgresDataDir, snapshotFile, postgresDataPath, postgresDataPath, nodeps.PostgresConfigDir, nodeps.PostgresConfigDir)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also impossible to understand or debug. Maybe can be simplified.

@rfay

rfay commented Sep 26, 2025

Copy link
Copy Markdown
Member Author

@rfay

rfay commented Sep 26, 2025

Copy link
Copy Markdown
Member Author

Last commit doesn't work at all, but suggests the path. Just bad Dockerfile layer building, invalid.

How This PR Solves The Issue: Adjusts Go-side Dockerfile template generation to resolve restore_command path for Postgres 10-18 and ensures target directory exists before writing recovery.conf.
Manual Testing Instructions: Rebuild db image then run ddev start and snapshot restore against Postgres 10 and Postgres 18 projects.
Automated Testing Overview: Not run; golangci-lint/go test blocked in sandbox.
Release/Deployment Notes: None.

Co-authored-by: ChatGPT <[email protected]>
@rfay

rfay commented Sep 29, 2025

Copy link
Copy Markdown
Member Author

The problem with test failures of lima and colima here is they don't allow mounts on anything but the home directory by default, and the test is assuming a mount from elsewhere. Fiddling with it.

@stasadev stasadev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, I have a small suggestion to make it simpler to check the PG_VERSION.

Comment thread pkg/ddevapp/db.go Resolved
Comment thread pkg/ddevapp/db.go Resolved
Comment thread pkg/ddevapp/config.go
Comment on lines +1250 to +1251
mkdir -p %[2]s
echo "restore_command = 'true'" >>%[1]s

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I didn't know about this syntax.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nor did I!

Comment thread pkg/ddevapp/db_test.go Resolved
Comment thread pkg/ddevapp/db_test.go Resolved
Comment thread pkg/ddevapp/ddevapp.go Resolved
Co-authored-by: Stanislav Zhuk <[email protected]>
@rfay

rfay commented Sep 30, 2025

Copy link
Copy Markdown
Member Author

I manually tested (WSL2), seems fine.

@rfay
rfay merged commit 40fad69 into ddev:main Sep 30, 2025
@rfay
rfay deleted the 20250925_rfay_postgres_18 branch September 30, 2025 17:01
@tyler36 tyler36 mentioned this pull request Oct 22, 2025
4 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants