PostgreSQL change default port

A port is a place to which other peripheral devices are attached. For each purpose, the port is different. A port is a communication endpoint physically, but if we talk about its software level, then in any operating system, it identifies the processes or the type of network services. Port is known as a transport port and has an address that is formed through a 16-bit number, called port number. Many protocols use these port numbers. TCP is a commonly used protocol. Similarly, in PostgreSQL, there is a specific port to which it listens. We will use a method to change the port of PostgreSQL in this guide.


We have used the Linux operating system for this purpose. To implement this article on Linux, you need to create a Linux running environment. First, install a Virtual box on your system. Install the file of Ubuntu, and then install it on a Virtual machine. After installation, configure it. Create a user and apply a password required for the future authentication of the user. The user must have access privileges to work on the Linux terminal because we will work on the command-line interface.

Similarly, the other software needed is PostgreSQL. The user must have PostgreSQL installed on the Linux system.

Postgresql port

The process of changing the default port in PostgreSQL will be discussed here. By default, during installation, you have seen that the port is 5432 and is present in the configuration file.

Moreover, if PostgreSQL is running on Linux/Unix environment, it will also need a local socket in addition to a TCP listening port. For the connection to the database, a local socket is recommended as it takes less time than creating a connection of TCP and transferring data. But it has a disadvantage, as it is only favorable if the application that is accessing the database is running on the same machine. On the Linux operating system, you can use the command listed below.

# Locate postgresql.conf

# /etc/postgresql/9.5/main/postgresql.conf

You can notice that the name of the server is mentioned in the path of the file. You can check it before opening it. There are a series of steps; you are required to follow them.

NOTE: we will change the PostgreSQL default port: 5432 to 5440

Step 1

The first step is to enlist the details of the port that is currently in use. And that is done by using a command on PostgreSQL. That will select all the records from the file pg_settings, where the name will equal the port.

The port number can be seen, and the source name is also written that is inside the configuration file.

Step 2

The second step is to check the information of the connection. This will reveal the information showing the name of the database as “Postgres” and having the host localhost with the port number “5432”.


Step 3

The very next step is to now show the configuration file. The port number is written inside the configuration file.

Step 4

Now we will display the path of the configuration file with the help of a grep command. The port number will be displayed. You will see the results of this command; a message along with the port number is displayed that if we change the port, the system will require a restart.

# cat /pgdata/data/postgresql.conf | grep ‘port’

We will update the port now. Again, use the same command and, this time, enter the port number you want to replace with the previous one. We will add the new one that is ‘5440’.

$ cat /pgdata/data/postgresql.conf | grep ‘port’

After updating, PostgreSQL needs to get restarted. So we will restart the PostgreSQL service. First, we will stop it and restart again.


Step 5

The next step is to check whether the port is updated or not. Go to the Linux terminal and then use the command to open the PostgreSQL with a new port number.

# psql –d postgres –d postgres –p 5440

To again enlist the details of the new port and to ensure the changes in it, we will enable the display on Postgresql.


This will show the description of the new port.


From the resultant value, you can see that the port number is changed from the default value 5432 to 5440.

Port 5432- already in use

When you are installing PostgreSQL for the first time on your system, the most common problem is that two or more different versions of the PostgreSQL server are running simultaneously on the system. But this is not mandatory. This problem can also occur if any other application is using port 5432. If you want to check which application is using this port, then use the below command

# sudo lsof -i: 5432

After running this, you have to enter a password, and then you will see all the applications that are currently running on that port. You will see the PostgreSQL currently running on the default port. The only method to get rid of this problem is to cancel all the PostgreSQL processes that are currently running. For this purpose, use the below command

# sudo pkill –u Postgres

The reason behind doing this step is that PostgreSQL is usually the only app that is interested in using port 5432. But if you come to know that there is some other application than the PostgreSQL running on port 5432, check the activity monitor and see which application is interrupting the PostgreSQL server and then close it from there.


This tutorial is written to target those end users that are interested in changing the default port to any other of their choice. Moreover, a solution to commonly existing errors is also given that usually occurs during PostgreSQL installation on the system. The step-by-step procedure of updating the port will help the user change the default port number of PostgreSQL with the new one.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.