Wednesday, October 26, 2011

How to Start and Stop PostgreSQL Service (Actually, Any Service) on Windows 7

On Windows XP, if you were logged in as a user with administrative rights, you could simply run cmd to start what in the old days we used to call a "DOS box" and run something like this:
    NET START postgresql
    NET STOP postgresql
from the Windows command prompt to start or stop PostgreSQL database server (or any other Windows service).

On Windows 7, however, it is a little more complicated than that. The problem is that, if you simply run cmd and try to start or stop a service, you will most probably get this error message:
System error 5 has occurred.
Access is denied.


To overcome this, you need to start a "DOS box" as Administrator since, to the best of my knowledge, one cannot run a single command as Administrator inside the command prompt window opened under an account with fewer rights (there are no su and sudo commands in Windows).



One option is to click the "Start" button, type cmd into the "Search programs and files" box and hit Ctrl+Shift+Enter instead of just Enter. This will start a "DOS box" with administrative rights. Now, in this DOS box, you can run one of these commands (replacing the "9.1" with whatever your actual version of PostgreSQL is):
    NET START postgresql-9.1 (for the 32-bit version)
    NET STOP postgresql-9.1 (for the 32-bit version)
    NET START postgresql-x64-9.1 (for the 64-bit version)
    NET STOP postgresql-x64-9.1 (for the 64-bit version)
to start and stop PostgreSQL.

Of course, you can also start and stop other services like that. For example, run
    NET START MySQL to start MySQL database server
    NET STOP MySQL to stop MySQL database server
or
    NET START Apache2.2 to start Apache 2.2 web server.
    NET STOP Apache2.2 to stop Apache 2.2 web server.



If you are a slave to convenience, there is another way: create a shortcut to start the DOS box as Administrator. Here are the steps:
  • Right-click anywhere on your desktop.
  • From the drop-down menu select "New", then - "Shortcut".
  • In the first "Create Shortcut" dialog box, type in cmd and click "Next".
  • In the next "Create Shortcut" dialog box, give your new shortcut a meaningful name (e.g., "CMD as Admin" or "DOS box as Admin"). Click "Finish". The new shortcut will be created.
  • Right-click the newly created shortcut and select "Properties" from the drop-down menu.
  • On the "Shortcut" tab (opens by default), click the "Advanced..." button.
  • In the pop-up dialog box, check the "Run as administrator" checkbox. Click the "OK" button.
  • Click the "Apply" button. Then - the "OK" button.
Again, you can use this DOS box to start and stop other services as well (see the examples above).



If you are really lazy, like yours truly, there is yet another way: create desktop shortcuts for starting and stopping PostgreSQL. Here is how (pretty much the same as above):
  • Right-click anywhere on your desktop.
  • From the drop-down menu select "New", then - "Shortcut".
  • In the first "Create Shortcut" dialog box, type in NET START postgresql-9.1 (replace "9.1" with "x64-9.1", "9.0" or whatever your version of PostgreSQL is). Click "Next".
  • In the second "Create Shortcut" dialog box, give your new shortcut a meaningful name (e.g., "Start PostgreSQL Server"). Click "Finish". The new shortcut will be created.
  • Right-click the newly created shortcut and select "Properties" from the drop-down menu.
  • On the "Shortcut" tab (opens by default), click the "Advanced..." button.
  • In the pop-up dialog box, check the "Run as administrator" checkbox. Click the "OK" button.
  • Click the "Apply" button. Then - the "OK" button.
  • Create another shortcut exactly as the one above. Just replace "NET START" with "NET STOP" and call it something like "Stop PostgreSQL Server".
Again, you can use the above steps to create shortcuts to start and stop any Windows service.



All of the above should also apply to Windows Vista.

10 comments:

polandroide said...

You are da' man.

Ira Portman said...

You are welcome, polandroide.

Anonymous said...

Thanks man.Good work

Ira Portman said...

You are welcome. Glad to save others the pain of learning the hard way.

Anonymous said...

Thank you so much for your post. It works!

Ira Portman said...

You are most welcome!

Anonymous said...

Thank you so much :)

Ira Portman said...

You bet!

david prasetyo said...

it works perfectly. thanks

Ira Portman said...

You are welcome, David.