3

My requirements are as follows:

  1. Once the application starts the PostgreSQL service will be started by the application
  2. Once the application closes the PostgreSQL service will be closed by the application
  3. ... so i will be taking care of the PostgreSQL setup and running the scripts and starting the service etc

How i am doing this at present is:

  1. when i start PostgreSQL in a new process i am redirecting the RedirectStandardError and RedirectStandardOutput, it is a silent start, user cannot see the command window etc

The problem is,

  • When I coded this I looked for message strings and only supported English. In other words, I used to look for the string success in RedirectStandardOutput but now we are supporting multiple languages so the comparison fails.

Is there any way i can find out whether PostgreSQL set up was successfully started and PostgreSQL service is running or not?

I am starting PostgreSQL by calling pg_ctl.exe in separate process.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • What are you going to do: 1) if the user (or users) will start more than one instances of your application? 2) if the user won't have enough access rights to start/stop services? Also, what way to start service are you talking about, when you're mentioning stderr & stdout redirection? – Dennis Jun 06 '12 at 05:45
  • all the details regarding 1) if the user (or users) will start more than one instances of your application? 2) if the user won't have enough access rights to start/stop services? are taken care. i am starting postgre by calling pg_ctl.exe in separate process – Raghunandan Ghagarvale Jun 06 '12 at 05:56
  • Are you using PostgreSQL's setup to install, or are you just bundling the zip file of executables with your program? Given the way you want total control I'd do the latter - I'd bundle the PostgreSQL executables and avoid creating a `postgres` user, service account, etc entirely. – Craig Ringer Jun 06 '12 at 11:14
  • You can see whether the PostgreSQL postmaster launched successfully by examining the return code of `pg_ctl.exe`. If there's a fault that allows the postmaster to launch but not function correctly (maybe a port conflict, permissions on the data directory, etc) pg_ctl won't know that, you need to examine the PostgreSQL logs. – Craig Ringer Jun 06 '12 at 11:15
  • i am using the zip i'm not installing the postgre sql in the client – Raghunandan Ghagarvale Jun 07 '12 at 09:29

6 Answers6

4

(we are doing something similar) you star the Postgres service by using this batch file

"C:\Program Files\PostgreSQL\9.0\bin\pg_ctl.exe"  -D "C:\Program Files\PostgreSQL\9.0\data" start

and for [Stopping] the service

"C:\Program Files\PostgreSQL\9.0\bin\pg_ctl.exe"  -D "C:\Program Files\PostgreSQL\9.0\data" stop

where C:\Program Files\PostgreSQL\9.0\bin\pg_ctl.exe is the location of your installation of PostgreSQl which u can get from

    HKEY_LOCAL_MACHINE\SOFTWARE\PostgreSQL\Installations\postgresql-9.0

Now on running the Batch file to check if the service is indeed running you can use this code from Process running check for postgres.exe if its running or not.

Also 1. checking-if-windows-application-is-running 2. how-can-i-know-if-a-process-is-running

  public bool IsProcessOpen(string name)
    {
 //here we're going to get a list of all running processes on
 //the computer
 foreach (Process clsProcess in Process.GetProcesses()) {
    //now we're going to see if any of the running processes
    //match the currently running processes. Be sure to not
    //add the .exe to the name you provide, i.e: NOTEPAD,
    //not NOTEPAD.EXE or false is always returned even if
    //notepad is running.
    //Remember, if you have the process running more than once, 
    //say IE open 4 times the loop thr way it is now will close all 4,
    //if you want it to just close the first one it finds
    //then add a return; after the Kill
    if (clsProcess.ProcessName.Contains(name))
    {
        //if the process is found to be running then we
        //return a true
        return true;
    }
}
//otherwise we return a false
return false;
}
Community
  • 1
  • 1
PresleyDias
  • 3,657
  • 6
  • 36
  • 62
2

Start a database connection, if it works, PostgreSQL is running. If not, something went wrong.

Frank Heikens
  • 117,544
  • 24
  • 142
  • 135
1

You're trying to use PostgreSQL as an embedded database for bundling with an application completely invisibly to the user.

This just isn't going to work very well. PostgreSQL isn't designed to work that way, and it shows:

  • The postmaster expects to run as a service or daemon spawned by pg_ctl, not be a child process of something else. It's designed to survive the termination of its parent process, which isn't what you want when embedding.

  • There is, of course, no way to embed PostgreSQL inside your executable or load it as a library.

  • The postmaster doesn't have an out-of-band management interface; all management is done via SQL level connections or via the config files and logs. Maybe that's a defect; it'd potentially be nice to have a simple management API that used simple interprocess communication (say, Pg's shared memory) to query the postmaster status without needing a working TCP connection and database engine. Right now, nothing like that exists, and for PostgreSQL's intended use case there isn't a strong argument to create it, because if PostgreSQL doesn't start the admin who installed it should fix that.

  • On Windows, PostgreSQL only supports communication over TCP/IP, and for that it needs a port that's unique host-wide. That doesn't work well if you might have multiple instances of your program running at once and want them to be independent.

Doing what you want is possible, just annoying and difficult because PostgreSQL isn't designed for use as an embedded database.

I'd actually love to see a low level diagnostics/status client for PostgreSQL that could use shared memory message passing with the postmaster to ask the postmaster for a simple status report. It'd be great for diagnosing installation problems, especially on all the broken Windows machines there are out there.

Maybe you'd consider prototyping the features you need after discussing them with the pgsql-hackers team and submitting a patch to the commitfest? PostgreSQL is open source, so you can potentially get the features you need added if you're willing to implement them and you're able to convince other people that they should be included.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
0

Start/stop services using ServiceController class.

Dennis
  • 37,026
  • 10
  • 82
  • 150
0

Link this will help you.Before that check your DB connection.or Start/Stop IIS Server(If running)

TechGuy
  • 4,298
  • 15
  • 56
  • 87
0

I would recommend evaluation this library: https://github.com/mysticmind/mysticmind-postgresembed

It is primarily intended for testing scenarios, but could serve other needs.

var instance = Guid.Parse("de6b862e-7ba2-4648-904b-a5b3db1c6d8e");
using var server = new PgServer("10.7.1", dbDir: @"x:\", instanceId: instance, port: 5500);
await server.StartAsync();

var connectionString = $"Server=localhost;Port={server.PgPort};User Id=postgres;Password=ignored;Database=postgres";
await using var connection = new NpgsqlConnection(connectionString);
await using var batch = new NpgsqlBatch(connection)
{
    BatchCommands =
    {
        new("drop table if exists SomeTable"),
        new("create table SomeTable(Id int primary key, Name varchar(50))"),
        new("insert into SomeTable values (1, 'Ronnie')"),
        new("insert into SomeTable values (2, 'Not Ronnie')"),
        new("select * from SomeTable")
    }
};

connection.Open();
await using var reader = await batch.ExecuteReaderAsync();
using var table = new DataTable();
table.Load(reader);
Ronnie Overby
  • 45,287
  • 73
  • 267
  • 346