Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a system user that is dedicated to my work. At work I need postgres, however I don't need it anywhere else. Is there a way to start the postgres service when the user logs in and stop it when i log out?

Note that the user has no admin priveleges and i currently cannot start the service with this user account. If I have to change it it's ok, but suboptimal.


During work I switch the user from work user to my normal user account. When I do this postgres should continue to run.

share|improve this question
up vote 3 down vote accepted

You could do this using Scheduled Tasks.

Use the On connection to user session trigger to run a batch script containing NET START [Service Name]

Use the On disconnect from user session trigger to run a batch script containing NET STOP [Service Name]

More info on Triggers here:

You should be able to set the tasks to run under an account which has permissions to start/stop services so you won't need to elevate your user's privileges.


Use the At log on trigger to run NET with START [Service Name] as the parameter

Use the On an event trigger (Custom settings, Event ID 4647, User initiated logoff, for the user) to run NET with STOP [Service Name] as the parameter

share|improve this answer
Just as a sidenote: You don't need to create a batch script, just use NET as the program and START [Service Name] as parameters. – Mene Jan 19 '12 at 13:51
Also I used the "At log on" trigger. There's no "At log out" so I used the proposed disconnect trigger; didn't test the connect trigger. I guess both are working, but the connect/disconnect sounds more "symmetric". Wonder why there's no log off counterpart for the log in trigger – Mene Jan 19 '12 at 13:57
Just noticed that this is not working for me. I'd need an "At log off" trigger since I'm switching around during work and postgres should continue to work as long as I don't log off the work-user. – Mene Jan 19 '12 at 14:10
If you use User Switching then you will need to use the connect trigger as the disconnect is triggered when you switch away and the logon will not trigger when switching back. – Shevek Jan 19 '12 at 14:13
Yes, but the service will be stopped when I switch away, but it needs to be running. I usually switch when i run integration tests and I need the database for this to work. – Mene Jan 19 '12 at 14:19

Another alternative is to use Group Policies.

Logged in as the postgres user:

gpedit.msc -> User Configuration -> Windows Settings -> Scripts (Logon/Logoff)

This will require the user to have permission to start/stop services

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .