from Hacker News

Test Postgres in Python Like SQLite

by wey-gu on 6/6/25, 12:56 AM with 52 comments

  • by jauco on 6/6/25, 5:34 AM

    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.
  • by WhyNotHugo on 6/6/25, 11:12 AM

    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.

  • by samwillis on 6/6/25, 6:56 AM

    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!

  • by benpacker on 6/6/25, 12:05 PM

    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...

  • by ptx on 6/6/25, 10:55 AM

    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?
  • by laurencerowe on 6/6/25, 5:03 AM

    This is running pglite in a node subprocess. Why not just run Postgres itself as a subprocess with a data directory in a tempdir?
  • by veggieroll on 6/6/25, 2:49 PM

    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.

  • by perrygeo on 6/6/25, 12:47 PM

    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.
  • by selimnairb on 6/6/25, 10:15 AM

    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”.
  • by wey-gu on 6/6/25, 1:54 AM

  • by murkt on 6/6/25, 3:59 AM

    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.
  • by ForHackernews on 6/6/25, 11:21 AM

    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/

  • by lucideer on 6/6/25, 9:50 AM

    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...

  • by gleenn on 6/6/25, 3:36 AM

    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
  • by jodiug on 6/6/25, 7:13 AM

    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?

  • by wg0 on 6/6/25, 7:50 AM

    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.

  • by ArcaneMoose on 6/6/25, 5:31 AM

    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!
  • by wey-gu on 6/6/25, 12:56 AM

    I need this in my use case, so built it after downstream e2e verified working!
  • by buremba on 6/6/25, 1:35 PM

    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?
  • by ewhauser421 on 6/6/25, 4:46 AM

    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?