0
0
PostgresqlHow-ToBeginner · 3 min read

How to Change Port in PostgreSQL: Simple Steps

To change the port in PostgreSQL, edit the postgresql.conf file and modify the port setting to your desired number. Then, restart the PostgreSQL server to apply the change.
📐

Syntax

The port setting is found in the postgresql.conf file. The syntax to change the port is:

port = 5432

Here, 5432 is the default port number. You can replace it with any valid port number you want PostgreSQL to listen on.

conf
port = 5433
💻

Example

This example shows how to change the PostgreSQL port from the default 5432 to 5433. After editing the configuration file, you must restart the PostgreSQL service for the change to take effect.

bash
# Locate the postgresql.conf file, usually in /etc/postgresql/14/main/ or similar
# Open the file and find the line starting with 'port'
port = 5433

# Restart PostgreSQL server (Linux example)
sudo systemctl restart postgresql

# Verify the new port by connecting
psql -h localhost -p 5433 -U your_username -d your_database
Output
psql (14.0) Type "help" for help. your_database=#
⚠️

Common Pitfalls

  • Not restarting the server: Changes to postgresql.conf only take effect after a server restart.
  • Port conflicts: Make sure the new port is not used by another service.
  • Firewall rules: Update firewall settings to allow traffic on the new port.
  • Wrong file location: The postgresql.conf file location can vary by installation method or OS.
bash
# Wrong: Changing port without restart
port = 5433
# PostgreSQL still listens on old port until restarted

# Right: After changing port
sudo systemctl restart postgresql
📊

Quick Reference

StepDescription
1Find and open postgresql.conf file
2Change the port value to desired number
3Save the file
4Restart PostgreSQL server to apply changes
5Update firewall and client connection settings if needed

Key Takeaways

Edit the port setting in postgresql.conf to change PostgreSQL's listening port.
Always restart the PostgreSQL server after changing the port for the change to take effect.
Ensure the new port is free and allowed by firewall rules to avoid connection issues.
The location of postgresql.conf varies by system; use SHOW config_file; in psql to find it.
Update client connection commands to specify the new port if it differs from the default 5432.