安装 Postgres – 无需 Docker/Brew/apt
Pip install Postgres – no Docker/Brew/apt

原始链接: https://github.com/leontrolski/postgresql-testing

**postgresql-testing** 是一个轻量级的 Python 库,用于在测试过程中管理独立的 PostgreSQL 实例,无需 Docker 或系统级安装等外部依赖。它利用 `postgresql-binaries` 提供“贴近底层”的接口,使开发人员能够轻松地为每个测试线程创建、模板化或归档全新的数据库。 主要功能包括: * **零配置**:通过 `pip install 'postgresql-testing[version]'` 安装,并可直接与 `pytest` 集成。 * **灵活性**:支持临时数据库、`initdb` 配置以及自定义用户管理。 * **命令行工具**:包含用于快速、临时托管数据库的 `postgresql-testing-serve` 命令(兼容 `uvx`),以及一个用于解析 `explain.dalibo.com` 查询计划的本地优先工具。 * **高性能**:专为速度设计,支持从模板或归档快速创建数据库,性能显著优于传统的基于容器的测试环境,特别是在使用内存盘(RAM disk)时。 无论您是运行本地集成测试还是分析查询性能,该工具都为 PostgreSQL 数据库管理提供了一种快速、可移植且开销极小的解决方案。

```Hacker News最新 | 过往 | 评论 | 提问 | 展示 | 招聘 | 提交登录Pip install Postgres – 无需 Docker/Brew/apt (github.com/leontrolski)4 分,发布者:leontrolski,1 小时前 | 隐藏 | 过往 | 收藏 | 2 条评论 帮助 wewewedxfgdf 14 分钟前 | 下一条 [–] Python 世界已经从 pip 转向 uv 了,至少目前是这个趋势。回复leontrolski 1 小时前 | 上一条 [–] 关于(滥用?)pypi 分发二进制文件的讨论 - https://discuss.python.org/t/use-of-pypi-as-a-generic-storag...回复 欢迎申请 YC 2026 年秋季批次!申请截止日期为 7 月 27 日。 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系方式 搜索: ```
相关文章

原文

Simple Postgres helpers for testing with Python - no docker, brew, apt, etc - uses postgresql-binaries. The interface is simple, but close enough to the metal that you can use it to eg. have a new database per testing thread/use a fresh database from a TEMPLATE or archive.

pip install 'postgresql-testing[15]'

Then to use with pytest, eg:

import postgresql_testing

@pytest.fixture(scope="session")
def db() -> Iterator[str]:
    config = postgresql_testing.DatabaseConfig.default("testing-db")
    postgresql_testing.initdb(config.directory, on_existing="use")
    with postgresql_testing.serve(config):
        postgresql_testing.ensure_user(config)
        postgresql_testing.create_database(config, on_existing="replace")
        yield config.dsn

There are various useful flags and things - the source code is short enough to just dive in.


You can spin up a fresh db for immediate testing with:

postgresql-testing-serve postgres://testing:@localhost:8421/my-db

Or if you want a throwaway Postgres from anywhere:

uvx --from 'postgresql-testing[16]' postgresql-testing-serve

Bundled for good measure is a local-first copy of explain.dalibo.com

Get your query plan by:

EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON) SELECT ...

Then copy and:

pbpaste | postgresql-testing-explain

There are a couple of helpers for creating/using template dbs and archives. I have some vague long term plan for some kind of "Docker layers for migrated databases" with clever caching (or not), but I'm not quite sure what it looks like yet.

Some benchmarking:

number of tables create from template dump to archive create from archive
100 80ms 80ms (0.2MB) 150ms
1000 500ms 300ms (2MB) 1100ms

On macos using a ramdisk, it is slightly quicker:

number of tables create from template dump to archive create from archive
100 70ms 70ms 120ms
1000 350ms 300ms 1000ms

See claims/advice from planet Go.

联系我们 contact @ memedata.com