electric-sql / pglite

Lightweight WASM Postgres with real-time, reactive bindings.
https://pglite.dev
Apache License 2.0
9.45k stars 204 forks source link

Is it possible to support setting username and password ? #401

Open DemoLiang opened 1 month ago

DemoLiang commented 1 month ago

Is it possible to support setting username and password like MySQL to allow connection to the database,and only this username/password can connect to the database

0xOlias commented 4 weeks ago

My mental model for PGlite is that when you run new PGlite(...), you establish a single connection / session. All queries to that PGlite instance use that connection. When you run instance.close(), it closes the connection.

As far as I can tell, PGlite supports the full Postgres user / role / permission system. However, you can only run one PGlite instance at a time for a given dataDir. So, unlike with a traditional Postgres server, only one database user can be connected at a time.

So, you can still create database users (as demonstrated in this test file) but to switch users, you have to close the current instance and open a new one, specifying the new username in the constructor.

I think it's technically possible to change users during a session with traditional Postgres, but it's uncommon and I doubt PGlite supports this.

Please correct me if I'm wrong @samwillis !