Skip to content

Add blog post on PostgreSQL state backend support for Pulumi #15271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 187 additions & 0 deletions content/blog/postgres-diy-backend/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
---
title: "Adding PostgreSQL State Backend Support to Pulumi: A Community Contribution Journey"
allow_long_title: true

# The date represents the post's publish date, and by default corresponds with
# the date and time this file was generated. Dates are used for display and
# ordering purposes only; they have no effect on whether or when a post is
# published. To influence the ordering of posts published on the same date, use
# the time portion of the date value; posts are sorted in descending order by
# date/time.
date: 2025-06-19T14:46:07+03:00

# The draft setting determines whether a post is published. Set it to true if
# you want to be able to merge the post without publishing it.
draft: false

# Use the meta_desc property to provide a brief summary (one or two sentences)
# of the content of the post, which is useful for targeting search results or
# social-media previews. This field is required or the build will fail the
# linter test. Max length is 160 characters.
meta_desc: "Learn about the community-driven journey to add PostgreSQL as a state backend option for Pulumi's DIY backend, enabling robust, scalable state management for self-hosted infrastructure."

# The meta_image appears in social-media previews and on the blog home page. A
# placeholder image representing the recommended format, dimensions and aspect
# ratio has been provided for you.
meta_image: meta.png

# At least one author is required. The values in this list correspond with the
# `id` properties of the team member files at /data/team/team. Create a file for
# yourself if you don't already have one.
authors:
- matan-baruch

# At least one tag is required. Lowercase, hyphen-delimited is recommended.
tags:
- features
- diy-backend
- postgresql
- state-management
- community


# The social copy used to promote this post on Twitter and Linkedin. These
# properties do not actually create the post and have no effect on the
# generated blog page. They are here strictly for reference.

# Here are some examples of posts we have made in the past for inspiration:
# https://www.linkedin.com/feed/update/urn:li:activity:7171191945841561601
# https://www.linkedin.com/feed/update/urn:li:activity:7169021002394296320
# https://www.linkedin.com/feed/update/urn:li:activity:7155606616455737345
# https://twitter.com/PulumiCorp/status/1763265391042654623
# https://twitter.com/PulumiCorp/status/1762900472489185492
# https://twitter.com/PulumiCorp/status/1755637618631405655

social:
twitter: "🎉 New PostgreSQL state backend support is now available in @PulumiCorp! Community contributor @matanbaruch shares the journey of adding robust database-backed state storage for DIY backends. #IaC #PostgreSQL #OpenSource"
linkedin: "Excited to share how PostgreSQL state backend support came to Pulumi through community contribution! This new feature enables robust, transactional state management for self-hosted infrastructure. Read about the development journey and technical implementation."

# See the blogging docs at https://github.com/pulumi/docs/blob/master/BLOGGING.md
# for details, and please remove these comments before submitting for review.
---

When managing infrastructure as code at scale, reliable state storage becomes critical. While Pulumi offers excellent cloud-based state management through Pulumi Cloud, many organizations need self-hosted solutions with the robustness and scalability of a proven database system. That's where PostgreSQL comes in as a compelling option for DIY backend state storage.

<!--more-->

## The Need for Database-Backed State Storage

Traditional DIY backends in Pulumi have relied on object storage systems like AWS S3, Google Cloud Storage, or Azure Blob Storage. While these work well for many use cases, they have limitations when it comes to handling very large state files, complex locking mechanisms, and transactional guarantees that some enterprise environments require.

PostgreSQL stood out as an excellent candidate for state storage due to its:

- **Large object support**: Ability to handle substantial state files without size constraints
- **ACID compliance**: Robust transactional guarantees for state consistency
- **Mature ecosystem**: Well-established tooling and operational practices
- **Scalability options**: From single instances to complex replication setups
- **Security features**: Comprehensive authentication and authorization capabilities

## The Community Contribution Process

This feature came to life through [PR #19581](https://github.com/pulumi/pulumi/pull/19581/files), which addressed a long-standing community request tracked in [issue #5632](https://github.com/pulumi/pulumi/issues/5632). The development process showcased the collaborative nature of open-source development, with multiple rounds of feedback, testing, and refinement.

### Key Technical Challenges

The implementation required several technical considerations:

**1. Integration with Go Cloud Development Kit (CDK)**

The PostgreSQL backend needed to integrate seamlessly with Pulumi's existing blob storage abstraction layer. This was achieved by implementing the `blob.BucketURLOpener` interface:

```go
// URLHandler is a URL opener for PostgreSQL URLs.
type URLHandler struct{}

// OpenBucketURL implements blob.BucketURLOpener.
func (p URLHandler) OpenBucketURL(ctx context.Context, u *url.URL) (*blob.Bucket, error) {
pg, err := NewPostgresBucket(ctx, u)
if err != nil {
return nil, err
}
return pg.Bucket(), nil
}
```

**2. Database Schema Design**

The implementation uses a simple but effective schema for storing state data:

```sql
CREATE TABLE IF NOT EXISTS pulumi_state (
key TEXT PRIMARY KEY,
data JSONB NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
```

This design allows for efficient key-based lookups while leveraging PostgreSQL's native JSON support for flexible state data storage.

**3. Cross-Platform Testing**

One of the significant challenges was ensuring the implementation worked across different operating systems and CI environments. The solution involved using [testcontainers-go](https://golang.testcontainers.org/modules/postgres/) to spin up PostgreSQL instances during testing, with appropriate skip conditions for environments where Docker isn't available:

```go
func skipIfDockerNotAvailable(t *testing.T) {
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
t.Skip("Skipping test: Docker not available on this platform in CI")
}

// Additional Docker availability checks...
}
```

## Performance and Limitations

While PostgreSQL provides excellent reliability and consistency guarantees, there are some trade-offs to consider:

**Advantages:**
- Excellent performance for small to medium-sized state files
- Strong consistency guarantees
- Mature backup and recovery options
- Comprehensive monitoring capabilities

**Limitations:**
- May be slower than object storage for very large state files
- Requires database infrastructure management
- Signed URLs (used for state permalinks) are not supported
Comment on lines +134 to +147
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd like to get some feedback from PM/PMM at Pulumi on the comparison of this state backend to Pulumi cloud and make sure we properly highlight the trade offs. Let me get back to you.


## Getting Started

Using the PostgreSQL backend is straightforward:

1. **Login to your PostgreSQL backend:**
```bash
pulumi login postgres://pulumi_user:secure_password@localhost:5432/pulumi_state?sslmode=require
```

2. **Use Pulumi normally:**
```bash
pulumi up
# Your state is now stored in PostgreSQL!
```

## Community Impact

This contribution demonstrates the power of community-driven development in open source projects. It addresses a real need expressed by Pulumi users while maintaining the high quality standards expected from the project.

The implementation follows Pulumi's architectural patterns and coding standards, making it a seamless addition to the existing codebase. The comprehensive documentation and testing ensure that future maintainers can easily understand and modify the code.

## What's Next?

The PostgreSQL backend opens up several possibilities for future enhancements:

- **High Availability**: Support for PostgreSQL clustering and replication
- **Performance Optimizations**: Caching strategies and connection pooling
- **Advanced Features**: Custom backup strategies and state analytics
- **Multi-tenant Support**: Isolation patterns for multiple teams or environments

## Conclusion

The addition of PostgreSQL state backend support to Pulumi represents more than just a new feature—it's a testament to the collaborative nature of open source development and the power of community contributions. By providing enterprise-grade state storage options while maintaining the simplicity that makes Pulumi great, this feature enables organizations to adopt infrastructure as code with confidence.

Whether you're running a small startup or managing infrastructure for a large enterprise, having robust, reliable state storage options is crucial. The PostgreSQL backend provides exactly that: a battle-tested, scalable solution that integrates seamlessly with Pulumi's existing architecture.

If you're interested in contributing to Pulumi or have ideas for new features, the community welcomes your contributions. This PR serves as an excellent example of how community members can make meaningful improvements to the project while learning from experienced maintainers and following established best practices.

Try out the PostgreSQL backend today and experience the benefits of database-backed state storage for your infrastructure as code workflows!
Binary file added content/blog/postgres-diy-backend/meta.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions data/team/team/matan-baruch.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
id = "matan-baruch"
name = "Matan Baruch"
title = "Software Engineer and Pulumi Community Contributor"
status = "guest"

[social]
github = "matanbaruch"
linkedin = "matan-baruch-7a81b6a9"
Binary file added static/images/team/matan-baruch.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.