Test Postgres in Python Like SQLite

  • There’s also https://testcontainers.com/ not sure about the speed difference but testcontainers has never felt slow to me when using it from node js unittests.

  • I can't imagine a wasm running inside nodejs being faster than native code that's been optimised for decades.

    > No PostgreSQL install needed—just Node.js

    postgres is 32MB, nodejs is 63MB. I know that 31MB isn't a huge deal for most folks, but it's hard to see as "doesn't require postgres" as a selling point when you need something else that's twice the size instead.

  • Awesome work Wey! Love that you're building this!

    I work on PGlite, we have an experimental WASI build that can run in a WASI runtime, it should enable dropping the Node requirement. It lacks error handling at the moment (WASM has no long jump, and Postgres uses that for error handling - Emscripten has hacks that fix this via JS), and so we haven't yet pushed it far.

    Do ping me on our Discord and I can point you towards it.

    Happy to answers any PGlite questions while I'm here!

  • I have this setup and integrated for Node/Bun -

    This is an example of a unit test of an API route on a fully isolated WASM backed Postgres - very few lines of code, and all your API unit tests can run fully in parallel without any shared state: https://github.com/ben-pr-p/bprp-react-router-starter/blob/m...

    This is all of the code needed to use Postgres in prod and PGLite in test/dev: https://github.com/ben-pr-p/bprp-react-router-starter/blob/m...

  • How does running PostgreSQL compiled to WebAssembly reduce "the overhead of a full PostgreSQL installation"? Couldn't a native version be configured similarly and avoid the additional overhead of WebAssembly, Node.js and npm?

  • This is running pglite in a node subprocess. Why not just run Postgres itself as a subprocess with a data directory in a tempdir?

  • Yo, this installs npm packages at runtime. Very not cool IMO. You should disclose this prominently in the README.

    This is a nice project idea. But, you should use a Python WASM interpreter to run the PostgreSQL WASM.

  • For Clojure and Java apps, check out Zonky (https://github.com/zonkyio/embedded-postgres). It provides a similar experience on the JVM, but instead of containers or WASM, you're running an embedded native binary.

  • I just use pytest-docker-compose, then I don’t need to bother with NPM. I usually don’t like “magic”, but pytest’s fixtures are so powerful I’m okay with a little bit of “magic”.

  • Roadmap of v1

    https://github.com/wey-gu/py-pglite/issues/4

  • I wonder if it’s possible to compile Postgres directly into a Python extension instead of WASM. Just import it and go forth, no node dependency, nothing.

  • Is this about testing Python that uses Postgres?

    Because if you're really interested in testing postgres you can test PG in PG: https://pgtap.org/

  • This seems great but:

    > The library automatically manages PGlite npm dependencies.

    I'm sorry what? Please don't do this. There is no way you can do this in a way that:

    (a) is compatible with every pre-existing in-repo NodeJS setup

    (b) plays nicely with all SCA automation configurations (Dependabot, etc.)

    ---

    Edit:

    After closer inspection there's a config[0] to disable this which defaults to True, but no documentation on how to manage required Node deps when setting that value to false.

    I would strongly suggest the authors default this to False with accompanying docs in the README

    Overall though I do like the simplicity & readability of the codebase - it makes these gotchas really easy to find & verify even without docs - will definitely be using this.

    [1] https://github.com/wey-gu/py-pglite/blob/b821cf2cfeb2e4bc58b...

  • I would love something like this for databases like Snowflake where there isn't an easy way to test without hitting a prod SF server

  • This is great, running tests with a database covers a ton of behaviors that are otherwise hard to test, and having the database in memory allows CI/CD to run those tests without downloading and building containers.

    It does depend on SQLAlchemy. Can this also be used with asyncpg? Is it on the roadmap?

  • What a library! Just five years ago couldn't have imagined Postgress could be running in browser.

    And now this.

    Going to use right away.

  • I recently struggled with PGlite for a while and ended up using full postgres via https://github.com/leinelissen/embedded-postgres which worked like a charm!

  • I need this in my use case, so built it after downstream e2e verified working!

  • Amazing, this is what I was trying to find for the last few weeks! I wonder if it's possible to run WASM directly from Python instead of the subprocess approach, though?

  • Nice! Ive been wanting to try this ever since PGLite came out.

    Anything that won’t work if you tried this as a drop in replacement for a full PG instance in tests? Maybe extensions? Any benchmarks?

  • [dead]