12

My PostgreSQL default port is 5432, I want to change default port by using command line (in Windows 7).

I saw this solution before: https://stackoverflow.com/questions/187438/want-to-change-pgsql-port

I tried running the following command, but it failed:

postgres --p 5431

Could someone tell me how to change port in using command line?

10

Since you're on Windows and are probably starting PostgreSQL as a Windows service, you'll need to edit postgresql.conf (inside your data directory) to set the new port there, then restart the postgresql service using the Services control panel or (as an Administrator) the net service command.

You can use a programmatic text editing tool to change postgresql.conf if you need to automate this. On a Linux/unix box you'd use a tool like sed or awk, but on Windows I imagine you'll be using VBS, powershell, or an add-on tool like Perl.

You'll need to change the connection settings in tools like PgAdmin-III to use the new port.

If you were starting PostgreSQL manually using pg_ctl you could set the PGPORT environment variable or pass a config option on the command line. This won't work when you're launching it as a Windows service.

4
  • I'm using "Swiss File Knife" to do replacement in text files on Windows. – a_horse_with_no_name May 3 '13 at 8:07
  • @a_horse_with_no_name Useful tool when you don't have fileutils/coreutils/etc; thanks for the tip. – Craig Ringer May 3 '13 at 8:30
  • So I can't use command line to change pgsql port in Windows? – Nick Hung May 7 '13 at 2:12
  • @NickHung Yes, you can, as explained above - use a text processor to change the config file, or don't run PostgreSQL as a Windows service and instead start it with pg_ctl. – Craig Ringer May 7 '13 at 5:05
8
  • go to ..\PostgreSQL\9.0\data and open the file postgresql.conf in text editor/notepad
  • search for port parameter .eg: port = 5433
  • edit this to your port number.
  • go to run type services.msc and restart postgresql service.

you can check wether the parameter is set or not via query tool. just execute show port query. it will display your current port number. If are using pgAdmin to connect to db make sure you changed your port number in connection settings.

0

A solution that does not require any text replacement or similar techniques is to configure the different port in a separate config file, then append an include directive to the postgresql.conf file.

e.g.

echo port = 5431 > \path\to\datadir\custom.conf
echo include custom.conf>> \path\to\datadir\postgresql.conf

(Of course \path\to\datadir\ needs to be replaced with the correct path)

The first echo command creates a new file with just the port configuration. The second one appends an include directive to the existing postgres config file. As the "later" settings override "earlier" settings in the file, any port configuration done in postgresql.conf will be overwritten with the one in the custom config file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.