Skip to content

Support custom embedded PostgreSQL bundles with configurable specs, download sources, and cache isolation #6015

Open

Description

@mathis6787

Problem to Solve

Serverpod's embedded PostgreSQL is a great addition for local development and testing. However, the current implementation makes it difficult to use custom PostgreSQL builds, particularly when a project requires additional extensions or a different PostgreSQL version.

I identified several related limitations:

  1. Hardcoded bundle specifications: publishedBundleSpecs in bundle_spec.dart defines the supported PostgreSQL versions, bundle revisions, and PostGIS/pgvector versions. Users cannot supply their own specifications through configuration.
  2. Limited download sources: SERVERPOD_PG_BUNDLE_REPO allows overriding the GitHub repository, but the download URL still follows a fixed GitHub Releases structure. Arbitrary HTTPS URLs are not supported.
  3. Fixed bundle manifest validation: ServerpodBundleArtifact.validateExtracted() verifies PostgreSQL, revision, platform, PostGIS, and pgvector versions against the predefined BundleSpec. There is no general mechanism for declaring and validating additional bundled extensions.
  4. Potential cache collisions: ServerpodBundleArtifact.cacheKey uses only spec.bundleId (<postgres-version>-r<revision>). Different bundles from different sources can therefore share the same cache location. Additionally, BinaryStore.ensure() considers an existing .meta.json sufficient for a cache hit without checking whether the cached artifact matches the requested source.
  5. PostgreSQL configuration: Some custom extensions require shared_preload_libraries, but postgres_conf_builder.dart currently generates shared_preload_libraries = '' in Serverpod's managed configuration block.

As a result, developers who need a custom PostgreSQL build must continue using an external PostgreSQL with Docker, despite the embedded database functionality already providing most of the necessary infrastructure.

Proposal

I propose adding first-class support for custom embedded PostgreSQL bundles while preserving the existing behavior for official Serverpod bundles.

1. Custom BundleSpec

Allow developers to supply their own bundle specifications without modifying publishedBundleSpecs.

A custom specification should support:

  • PostgreSQL version and bundle revision.
  • Arbitrary bundled extensions and their versions, rather than a fixed set of extension fields.
  • Platform-specific artifacts.
  • Validation of declared versions and bundle metadata.
  • A unique identity for the custom bundle.

The existing official specifications would remain the default when no custom configuration is provided.

2. Configurable download sources

Extend the existing SERVERPOD_PG_BUNDLE_REPO mechanism to support arbitrary HTTPS download URLs, including GitHub Releases, self-hosted servers, or other artifact repositories.

This could be implemented using environment variables, serverpod config etc...

For example, a custom bundle manifest could conceptually look like:

{
  "id": "custom-pg17-textsearch",
  "postgres": "17.7.0",
  "revision": 1,
  "extensions": {
    "pg_textsearch": "0.5.0"
  },
  "artifacts": {
    "macos-arm64": {
      "url": "https://example.com/postgres-macos-arm64.tar.xz",
      "sha256": "<expected-sha256>"
    }
  }
}

This is only an illustrative format, not a suggestion to adopt this exact schema.

3. Source-aware cache identity

Custom bundles should not accidentally share cache entries with official bundles or other custom distributions.

Currently, the cache key is derived from the PostgreSQL version and bundle revision, without incorporating the source or distribution identity.

Possible improvements include:

  • Incorporating a custom bundle identifier or namespace into the cache key.
  • Optionally incorporating a pinned artifact checksum.
  • Validating cached artifact identity before reusing it.
  • Preserving backward compatibility with existing official bundle caches.

The existing .meta.json already records source_url and sha256, which could potentially help with validation.

4. Integrity and version validation

Custom bundles should retain the security and verification guarantees of official Serverpod bundles.

Ideally, this includes:

  • SHA-256 verification against an explicitly trusted expected checksum.
  • Validation of PostgreSQL and declared extension versions against the bundle manifest.
  • Validation of platform compatibility.
  • Clear errors when the bundle contents do not match the supplied specification.
  • Consistent handling of bundle revisions and cache invalidation.

Where practical, validation could also verify installed PostgreSQL binaries and extension metadata rather than relying exclusively on self-declared manifest values.

5. Support extensions requiring preloading

Some extensions require PostgreSQL configuration changes, particularly shared_preload_libraries.

It would be useful to expose a supported way to configure this setting for embedded PostgreSQL, rather than requiring modifications to the generated managed configuration block.

This could also be addressed in a separate issue if it is considered outside the scope of custom bundle distribution.

Backward compatibility

The current official bundle download, caching, validation, and startup behavior should remain unchanged when no custom bundle is configured.

Use Case

In my project, I use PostgreSQL 17 with the pg_textsearch extension for BM25 full-text search.

Currently, I run a custom PostgreSQL Docker image to provide the required extension.

I would like to use Serverpod's embedded PostgreSQL for local development and integration testing while keeping the same PostgreSQL version and extensions.

An ideal workflow would be:

  1. Build PostgreSQL with the required extensions.
  2. Package it using a Serverpod-compatible bundle structure.
  3. Publish the bundle to my own GitHub Releases or another artifact host.
  4. Declare the custom bundle specification and download source in my project's configuration.
  5. Run serverpod start and let Serverpod automatically download, verify, cache, and start the custom PostgreSQL distribution.

This would preserve the main advantages of embedded PostgreSQL.

Alternatives

The following alternatives are currently available:

1. Continue using Docker

2. Install PostgreSQL natively

This avoids Docker but requires developers to install, configure, and manage PostgreSQL and its extensions separately, losing the automatic lifecycle benefits of embedded PostgreSQL.

3. Fork or modify serverpod_embedded_postgres

Developers could modify publishedBundleSpecs, bundle download logic, and PostgreSQL configuration directly. However, this introduces maintenance overhead and makes upgrading Serverpod more complicated.

4. Use the existing SERVERPOD_PG_BUNDLE_REPO override

This is useful for mirrors or compatible custom bundles but does not solve the hardcoded specification, arbitrary URL, extension validation, or cache identity limitations.

SERVERPOD_PG_CACHE_DIR can isolate different distributions manually, but it does not provide automatic cache isolation based on bundle identity.

None of these alternatives provides the same out-of-the-box experience as the official embedded PostgreSQL bundles while supporting custom PostgreSQL distributions.

Additional context

Relevant source code:

The existing BinaryArtifact and BinaryStore abstractions already provide much of the required download, verification, and caching infrastructure, so supporting custom distributions may be possible without fundamentally changing the embedded PostgreSQL architecture.

I'd be interested in working on a PR for this if it's something that serverpod would like to support. I'd appreciate some guidance on the preferred approach before getting started, especially since this touches a critical part of the embedded PostgreSQL infrastructure.

How experienced are you with this library?

Intermediate - Familiar with the basics or have used it in a few projects

Are you interested in working on a PR for this?

  • I want to work on this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions