16

For a while i have my db running on a command window because im not figuring out how to run it as a windows service.

Since i have the zip file version downloaded. how can i register the pg_ctl command as a windows service?

By the way, im using the following line to start the server:

"D:/Program Files/PostgreSQL/9.0.4/bin/pg_ctl.exe" -D "D:/Program Files/PostgreSQL/9.0.4/db_data" -l logfile start

Thanks in advance.

thclpr
  • 5,778
  • 10
  • 54
  • 87

2 Answers2

31

Use the register parameter for the pg_ctl program.

The data directory should not be stored in Program Files, the location of %ProgramData% is e.g. a good choice.

pg_ctl.exe register -N PostgreSQL -U some_windows_username -P windows_password -D "%ProgramData%/db_data" ...

In newer versions of Postgres, a separate Windows account is no longer necessary, so the following is also sufficient

pg_ctl.exe register -N PostgreSQL -D "%ProgramData%/db_data" ...

Details are in the manual: http://www.postgresql.org/docs/current/static/app-pg-ctl.html

You need to make sure the directory D:/Program Files/PostgreSQL/9.0.4/db_data has the correct privileges for the windows user you specify with the -U flag.

Btw: it is a bad idea to store program data in Program Files. You should move the data directory somewhere outside of Program Files because Program Files is usually highly restricted for regular users - with a very good reason.

18

Just run 'Command Prompt' as windows administrator and run the below command:

pg_ctl.exe register -N PostgreSQL -D "D:/Program Files/PostgreSQL/9.0.4/db_data"

You don't need to specify a User and Password, as previous answers have suggested.

John
  • 1
  • 13
  • 98
  • 177
Geek669
  • 181
  • 1
  • 3
  • Hi, nice first answer, perhaps you could add an extra newline character before `pg_ctl.exe register -N postgres -D "D:/Program Files/PostgreSQL/9.0.4/db_data"` as I can see you've tried to make it highlighted by adding 4 spaces before it. – Troyseph Jul 02 '15 at 15:32
  • I'm getting error while running above service in command prompt in windows system. – sankar muniyappa Jun 01 '16 at 14:11
  • 2
    For Postgres 11 you'll want to modify the data location: `pg_ctl.exe register -N postgres -D "D:/Program Files/PostgreSQL/11/data"` – kernel Feb 28 '19 at 12:52