https://github.com/boustrophedon/pgtemp Skip to content Toggle navigation Sign in * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code Explore + All features + Documentation + GitHub Skills + Blog * Solutions For + Enterprise + Teams + Startups + Education By Solution + CI/CD & Automation + DevOps + DevSecOps Resources + Learning Pathways + White papers, Ebooks, Webinars + Customer Stories + Partners * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles Repositories + Topics + Trending + Collections * Pricing Search or jump to... Search code, repositories, users, issues, pull requests... Search [ ] Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. [ ] [ ] Include my email address so I can be contacted Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly Name [ ] Query [ ] To see all available qualifiers, see our documentation. Cancel Create saved search Sign in Sign up You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} boustrophedon / pgtemp Public * Notifications * Fork 1 * Star 98 * Rust library and daemon for easily starting postgres databases per-test without Docker License MIT license 98 stars 1 fork Branches Tags Activity Star Notifications * Code * Issues 0 * Pull requests 0 * Actions * Projects 0 * Security * Insights Additional navigation options * Code * Issues * Pull requests * Actions * Projects * Security * Insights boustrophedon/pgtemp This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. master BranchesTags Go to file Code Folders and files Name Name Last commit Last commit message date Latest commit History 13 Commits .github/workflows .github/ workflows examples examples src src tests tests .gitignore .gitignore CHANGELOG.md CHANGELOG.md Cargo.lock Cargo.lock Cargo.toml Cargo.toml LICENSE LICENSE Makefile Makefile README.md README.md TODO TODO View all files Repository files navigation * README * MIT license pgtemp Coverage Status CI Status crates.io docs.rs pgtemp is a Rust library and cli tool that allows you to easily create temporary PostgreSQL servers for testing without using Docker. The pgtemp Rust library allows you to spawn a PostgreSQL server in a temporary directory and get back a full connection URI with the host, port, username, and password. The pgtemp cli tool allows you to even more simply make temporary connections, and works with any language: Run pgtemp and then use its connection URI when connecting to the database in your tests. pgtemp will then spawn a new postgresql process for each connection it receives and transparently proxy everything over that connection to the temporary database. Note that this means when you make multiple connections in a single test, changes made in one connection will not be visible in the other connections, unless you are using pgtemp's --single mode. pgtemp supports loading (and dumping, in the library) the database to /from dumpfiles via pg_dump. Requirements You must install both the postgresql client and server packages. On Debian/Ubuntu, they are postgresql postgresql-client, on Fedora they are postgresql postgresql-server, and on Arch Linux they are postgresql postgresql-libs. Note also that Debian/Ubuntu install the standard postgres binaries into their own directory, so you must add them to your path. For an Ubuntu GitHub Actions runner, it looks like: steps: - name: Install postgres run: sudo apt-get install postgresql postgresql-client - name: Update path run: find /usr/lib/postgresql/ -type d -name "bin" >> $GITHUB_PATH To install the CLI tool, you must install it with the --features cli or --all-features options cargo install pgtemp --features cli Design pgtemp is a fairly simple program and there are other existing libraries like testing.postgresql for Python and pgtest for Go that all work the same way: * Do some setup, like creating temporary directories and copy files * Run the initdb program * Start the postgres server * Wait until postgres has started The novel idea (as far as I'm aware, although I also only found out about the above python/go libraries after coming up with the initial library idea) in pgtemp is the CLI/daemon which automatically provides connections to new temporary databases upon each connection. Examples CLI $ cargo install --all-features pgtemp # username, password, port, and database name are all configurable based on the provided connection URI $ pgtemp postgresql://localhost:6543/mytestdb starting pgtemp server at postgresql://postgres:password@localhost:6543/mytestdb $ psql postgresql://postgres:password@localhost:6543/mytestdb psql (16.1) Type "help" for help. postgres=# See examples/ directory for examples: * A python example with sqlalchemy and alembic, demonstrating usage with the pgtemp cli's normal and single modes Library use pgtemp::PgTempDB; use sqlx::postgres::PgConnection; use sqlx::prelude::*; #[tokio::test] fn cool_db_test() { let db = PgTempDB::async_new().await; let mut conn = sqlx::postgres::PgConnection::connect(&db.connection_uri()) .await .expect("failed to connect to temp db"); // ... do the rest of your test // db is shut down and files cleaned up upon drop at the end of the test } Examples: * A simple diesel example with axum * A more complicated "task queue" example using triggers and LISTEN /NOTIFY with sqlx and axum See the tests/ directory for complete library usage. About Rust library and daemon for easily starting postgres databases per-test without Docker Resources Readme License MIT license Activity Stars 98 stars Watchers 1 watching Forks 1 fork Report repository Releases 1 tags Packages 0 No packages published Languages * Rust 97.3% * Makefile 2.7% Footer (c) 2024 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact * Manage cookies * Do not share my personal information You can't perform that action at this time.