Skip to content
Home » How to Change the Default Port in PostgreSQL?

How to Change the Default Port in PostgreSQL?

  • by

By default, PostgreSQL uses port 5432. But for security’s sake, I want to change the default port to another one.

 

Problem

How to change the default port in PostgreSQL?

 

Solution

If you want to see the port used by PostgreSQL, you can see it in the postgresql.conf file by using the command (I’m using Ubuntu distro and PostgreSQL version 18):

cat /etc/postgresql/18/main/postgresql.conf | grep 'port ='
Display the PostgreSQL port via postgresql.conf file

 

Or you can use the command below to see the PostgreSQL port:

sudo ss -ptuln | grep postgres
Display the PostgreSQL port via netstat

 

Or you can use the query in this post to see the port used by PostgreSQL:

SHOW PORT;
change the default port in PostgreSQL
Display the PostgreSQL port via query

 

From the images above, you can see that PostgreSQL uses port 5432. If you want to change the PostgreSQL port to port 6543, for example, then go to the /etc/postgresql/18/main/postgresql.conf file if you use the Ubuntu distro and PostgreSQL version 18, and change the port value from 5432 to 6432.

Warning
If you’re using a distro other than Ubuntu/Debian, you can search for the postgresql.conf file by using the command:

sudo find / -type f -name "*postgresql.conf"

 

Then restart PostgreSQL using the command:

sudo systemctl restart postgresql

 

After that, you can check the PostgreSQL port by using one of the commands above, and the PostgreSQL port should have changed according to the port you want as shown in the image below:

Display the PostgreSQL port via netstat after changing the port

 

Note

If you have changed the default port of PostgreSQL from 5432 to 6432, for example, then you don’t need to write the port in the Linux command to access PostgreSQL if you access from localhost: 

Access PostgreSQL from localhost after changing the default port

 

But, if you access PostgreSQL from another host, you have to write the option for the port, like in the picture below:

Access PostgreSQL from another host after changing the default port

 

References

stackoverflow.com
dbvis.com
geeksforgeeks.org

image_pdfimage_print
Visited 19 times, 1 visit(s) today

Leave a Reply

Your email address will not be published. Required fields are marked *