Changing PostgreSQL port is our adventure in this article, hence, flow these instructions to achieve the objective in clear four steps.
If you didn’t have an adventure with PostgreSQL, then read this basics article.
PostgreSQL also called Postgres is a powerful DBMS. moreover, It is available in most Linux distributions. therefore, It comes with the default values.
I usually ask a few questions about any new software installation. for example, I ask what is the default values and where is the location of configuration files.
Postgres default port
PostgresSQL default port is 5432, however, the installer will pick the next available port if it is not available. for example port 5433 … etc.
Postgres default installation does not allow external access. hence, local applications use a socket or network port is 5432 from localhost only.
To check the current port used by Postgres service by using “netstat“. in default configuration, it will show you the port mapped to localhost 127.0.0.1:5434
netstat -lnp | grep postgres
changing Postgres default port
Open postgres main configuration file postgresql.conf. This file located inside /etc/postgresql/<PG-Version>/main/ . To clarify, PGVersion will replaced by postgresSQL version.
Changing port for postgresSQL 9.5
To successfully change database port the following steps Stop database if running by executing the following command
systemctl stop postgresql
open postgresql.conf file
vi /etc/postgresql/9.5/main/postgresql.conf
Firstly you need to change the value of the paramter located in line 63, or you can search for word “port”. Then, under the section titled CONNECTIONS AND AUTHENTICATION.
Keep in mind, that make sure that the new port number is free.
Start the postgreSQL service using systemctl
systemctl start postgresql
PostgreSQL error log
You should the error log of after service is started. using
tail /var/log/postgresql/postgresql-9.5-main.log
Conclustion
In conclusion, If you want to change postgresql default port 5432, You should edit the file /etc/postgresql/9.5/main/postgresql.conf file.
Make sure to configure your application to use the new port to avoid application crash