How to run PostgreSQL as a service in windows?
I installed postgreSQL binaries in windows 7 32bit operating system; I can start the server from cmd but I cant run it as a windows service.
This is the error that I'm getting when I try to start the service manually:
"The postgreSQL service on local computer started and then stopped. some services stop automatically if they are not in use by other servces or programs"
All I need to do is; after booting to windows when I double clicked my java application I need to run my app smoothly without any database errors. I cannot do this because postgreSQL is not running as a windows service.
I found this in windows event viewer:
The description for Event ID 0 from source PostgreSQL cannot be found.
Either the component that raises this event is not installed on your local computer or the installation is corrupted.
You can install or repair the component on the local computer.
If the event originated on another computer, the display information had to be saved with the event.
The following information was included with the event:
postgres cannot access the server configuration file
"C:/Windows/system32/pgsql/data/postgresql.conf": No such file or directory
I googled about it but I was not able to find an answer.
Problem was solved path to the data directory was wrong in windows service file.
So I delete the service file from this method:
1)Run Regedit or Regedt32.
2)Go to the registry entry "HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services"
3)Look for the postgreSQL service that you want delete and delete it.
4)reboot the computer.
After that open the cmd (Run as administrator) Go to the postgreSQL bin directory and run this command to create a new windows service:
pg_ctl.exe register -N "PostgreSQL" -U "NT AUTHORITY\NetworkService" -D "C:/Program Files/postgresql/pgsql/bin/pgsql/data" -w
This worked for me. Hope this will help.
This is a note for a normal user. If using an official installer it should has a build-in service,
-
Win+R and type
services.msc
- Search Postgres service based on the version installed, e.g., "postgresql-x64-13 - PostgreSQL Server 13"
- Click stop, start, or restart the service option