0

I'm working on a c++ desktop application involving database management, and i'm facing few issues on how to start/stop the postgres server automatically after the application starts/stops.

we could register the service during the installation as "automatic" but if the application is off it would be a waste of resources. manually it could be done via the svc manager or the command line (pg_ctl -D ... start), but desktop applications do it automatically.

the general ideas i've been able to find on the internet were to register it as a service then start/stop it manually or run it from inside the code by running the command.

i would be very grateful if anybody could point me to a guide on the proper way to do it, or tell me how it's done.

whyn0t
  • 301
  • 2
  • 14
  • Why do you want to shut it down at all? – Adrian Klaver Feb 22 '23 at 21:09
  • @AdrianKlaver to optimize resources consumption, the application is for local usage ( one client and one server on the same machine), there is no need to have Postgres running all the time. – whyn0t Feb 22 '23 at 22:20
  • I think you're looking for [this](https://stackoverflow.com/questions/25269274/packaging-database-into-application-seamlessly-for-users). What you described already sounds pretty much like the proper way to do it. The reason resources might be scarce on good practice in this area is somewhat explained [here](https://stackoverflow.com/a/14314161/5298879) (same author, linked thread) - PostgreSQL isn't usually the first choice for an embedded/bundled db for a monolithic app. – Zegarek Mar 02 '23 at 20:12

2 Answers2

-1

Registering as a service is not a good idea, because:

  • Service is a system-wide.
  • If you are using a service, you generally assume that the service will run for a long time (even after closing the "front-end" application) or will be used by multiple applications.
  • You need additional privileges to control the service.

This is not your way if you want to run PostgreSQL built into your application.

Using pg_ctl is better. Your best bet would be to use the "standard" library, i.e. libpq to control the server. But lipq doesn't have such an API, because regular applications SHOULD NOT control a shared server.

I think, more correct way is to do this, like pg_ctl. You can look at the pg_ctl code: https://github.com/postgres/postgres/blob/master/src/bin/pg_ctl/pg_ctl.c#L924

No magic there and starting process is simple:

  1. Read options.
  2. Find Postgres executable.
  3. Run it.
  4. Wait.
  5. Check the status.

Function, which runs executable just use command interpreter to run Postgres executable: https://github.com/postgres/postgres/blob/master/src/bin/pg_ctl/pg_ctl.c#L440

P.S.:

Another possible option is to use SQLite, which is designed for embedding.

A.N.
  • 278
  • 2
  • 13
-1

I recommend you to look at "embedded-postgres-binaries" GitHub project which provides small-size binaries that have been then used by other projects (referred in README.md) to provide embedded support for Java, Rust, Go and JavaScript (Node.JS) languages.

There is still no easy-to-use support for C++ (yet) but these integrations mainly consists in starting the PostgreSQL server engine as a forked process with your own tuned light configuration from your application code and connecting to socket to submit commands and requests.

The "portable" binaries are published as artefacts in a Maven repository, but quite easy to retrieve.

Yves Martin
  • 10,217
  • 2
  • 38
  • 77
  • @whyn0t To notify answer for your bounty – Yves Martin Mar 04 '23 at 16:21
  • @whyn0t I have answered to the option "run it from inside the code by running the command" but definitely there is no specific existing C++ "guide on the proper way to do it, or tell me how it's done"... so you should really try on your own from this embedded option, there is low chance somebody will do the job for you. – Yves Martin Mar 11 '23 at 14:44