Show HN: I made pgdbtemplate to cut PostgreSQL test time by 1.5x using templates

github.com

2 points by andrei-polukhin 6 hours ago

Tired of your Go tests being slowed down by repetitive PostgreSQL database setup?

I built pgdbtemplate to solve exactly that. It uses PostgreSQL's native template databases to create fully migrated test databases in milliseconds instead of seconds.

The problem: Traditional test setup runs all your migrations for every single test. With a complex schema, this wastes minutes in every test run.

The solution: Create one "golden" template database. Every test gets its own isolated copy instantly via CREATE DATABASE ... TEMPLATE.

Benchmarks don't lie:

- 1.5x faster for complex schemas (5+ tables)

- 37% time saved creating 200 databases

- 17% less memory usage

- Performance gains increase with schema complexity

It's:

- Thread-safe and built for t.Parallel()

- Driver-agnostic: works with both pgx and pq

- Integrates with testcontainers-go

- MIT Licensed

I'd love feedback from the HN community, especially on:

- The API design (ConnectionProvider/MigrationRunner interfaces)

- Ideas for further performance optimizations

- Security audit of the SQL formatting code

Check out the code and benchmarks on GitHub: https://github.com/andrei-polukhin/pgdbtemplate