Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If possible how can one embed PostgreSQL?

If it's possible, I'm interested in being able to embed a PostgreSQL database, similar to sqllite. I've read that it's not possible. I'm no database expert though, so I want to hear from you.

Essentially I want PostgreSQL without all the configuration and installation. If it's possible, tell me how.

like image 737
pc1oad1etter Avatar asked Sep 13 '08 02:09

pc1oad1etter


People also ask

Can PostgreSQL be embedded?

[ Also on InfoWorld: How SQL can unify access to APIs ] For example, we say that Steampipe embeds Postgres. That's not technically true. You can't link Postgres into a binary application, but you can (as Steampipe does) deliver a binary that installs, runs, and cooperates with Postgres.


2 Answers

You cannot embed it, nor should you try.

For embedding you should use sqlite as you mentioned or firebird rdbms.

like image 53
Brian R. Bondy Avatar answered Sep 18 '22 22:09

Brian R. Bondy


Run postgresql in a background process.

Start a separate thread in your application that would start a postgresql server in local mode either by binding it to localhost with some random free port or by using sockets (does windows support sockets?). That should be fairly easy, something like:

system("C:\Program Files\MyApplication\pgsql\postgres.exe -D C:\Documents and Settings\User\Local Settings\MyApplication\database -h 127.0.0.1 -p 12345");

and then just connect to 127.0.0.1:12345.

When your application quits, you can always send a SIGTERM to your thread and then wait a few seconds for postgresql to quit (ie join the thread).

PS: You can also use pg_ctl to control your "embedded" database, even without threads, just do a "pg_ctl start" (with appropriate options) when starting the application and "pg_ctl stop" when quitting it.

like image 38
brcha Avatar answered Sep 18 '22 22:09

brcha