localhost:5432
localhost:5432
Port 5432 is the default port for PostgreSQL database server. PostgreSQL (often called Postgres) is a powerful open-source relational database system known for reliability, feature robustness, and performance. Unlike web servers, you connect to port 5432 using database clients and applications, not web browsers.
What is Port 5432?
Port 5432 is the standard PostgreSQL database communication port assigned by IANA.
Services Using Port 5432
- PostgreSQL Server - Advanced open-source database
- EnterpriseDB - Commercial PostgreSQL distribution
- Amazon RDS PostgreSQL - AWS managed PostgreSQL
- Google Cloud SQL for PostgreSQL - GCP managed service
- Azure Database for PostgreSQL - Microsoft managed service
- Citus - Distributed PostgreSQL extension
- TimescaleDB - Time-series database on PostgreSQL
Connect to PostgreSQL on Port 5432
psql Command Line Client
# Connect to PostgreSQL
psql -h localhost -p 5432 -U postgres
# Shorthand (defaults to localhost:5432)
psql -U postgres
# Connect to specific database
psql -h localhost -p 5432 -U postgres -d database_name
# Connect with password prompt
psql -h localhost -p 5432 -U postgres -W
# Connection string format
psql postgresql://username:password@localhost:5432/database_name
# After connection, enter password when prompted
Password for user postgres: ****
Check PostgreSQL Status
# Linux - Check PostgreSQL service
sudo systemctl status postgresql
sudo service postgresql status
# Mac - Check PostgreSQL
brew services list | grep postgresql
pg_ctl status -D /usr/local/var/postgres
# Windows - Check service
# Services.msc > Look for "postgresql-x64-xx"
sc query postgresql-x64-14
# Check if PostgreSQL is listening on port 5432
# Windows
netstat -ano | findstr :5432
# Linux/Mac
sudo lsof -i :5432
sudo netstat -tulpn | grep :5432
sudo ss -tulpn | grep :5432
Default PostgreSQL Credentials
| Installation |
Username |
Password |
Database |
| Linux (apt/yum) |
postgres |
(set during install) |
postgres |
| Mac (Homebrew) |
your_username |
(no password) |
postgres |
| Windows Installer |
postgres |
(set during install) |
postgres |
| Docker postgres:latest |
postgres |
(set with POSTGRES_PASSWORD) |
postgres |
Database Connection Strings
Node.js (pg library)
// npm install pg
const { Client } = require('pg');
const client = new Client({
host: 'localhost',
port: 5432,
user: 'postgres',
password: 'your_password',
database: 'mydb'
});
client.connect()
.then(() => console.log('Connected to PostgreSQL'))
.catch(err => console.error('Connection error', err));
// Execute query
client.query('SELECT * FROM users', (err, res) => {
if (err) throw err;
console.log(res.rows);
client.end();
});
// Using connection pool
const { Pool } = require('pg');
const pool = new Pool({
host: 'localhost',
port: 5432,
user: 'postgres',
password: 'password',
database: 'mydb',
max: 20,
idleTimeoutMillis: 30000
});
Python (psycopg2)
# pip install psycopg2-binary
import psycopg2
try:
connection = psycopg2.connect(
host='localhost',
port=5432,
user='postgres',
password='your_password',
database='mydb'
)
cursor = connection.cursor()
cursor.execute('SELECT version();')
version = cursor.fetchone()
print('PostgreSQL version:', version)
# Execute query
cursor.execute('SELECT * FROM users')
users = cursor.fetchall()
for user in users:
print(user)
cursor.close()
except psycopg2.Error as e:
print(f'Error: {e}')
finally:
if connection:
connection.close()
PHP (PDO)
<?php
try {
$dsn = "pgsql:host=localhost;port=5432;dbname=mydb";
$username = "postgres";
$password = "your_password";
$pdo = new PDO($dsn, $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected to PostgreSQL successfully\n";
// Execute query
$stmt = $pdo->query('SELECT * FROM users');
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($users as $user) {
echo $user['name'] . "\n";
}
} catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
?>
Java (JDBC)
// Add PostgreSQL JDBC driver to classpath
import java.sql.*;
public class PostgreSQLConnection {
public static void main(String[] args) {
String url = "jdbc:postgresql://localhost:5432/mydb";
String user = "postgres";
String password = "your_password";
try {
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println("Connected to PostgreSQL");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while (rs.next()) {
System.out.println(rs.getString("name"));
}
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Fix "Could not connect to server" on Port 5432
Error: Connection refused
PostgreSQL server is not running.
- Linux:
sudo systemctl start postgresql
- Mac:
brew services start postgresql
- Windows: Start service in Services.msc
- Check if PostgreSQL service is enabled at startup
- Review PostgreSQL logs for startup errors
# Start PostgreSQL service
# Linux
sudo systemctl start postgresql
sudo service postgresql start
# Enable on boot
sudo systemctl enable postgresql
# Mac (Homebrew)
brew services start postgresql@14
# Or manually
pg_ctl -D /usr/local/var/postgres start
# Windows
# Services.msc > postgresql-x64-xx > Start
# Or command line
net start postgresql-x64-14
# Check if started successfully
sudo systemctl status postgresql
Error: FATAL: password authentication failed
Wrong password or authentication issue.
- Verify username and password are correct
- Check pg_hba.conf authentication method
- User may not have permission to access database
- Password may need to be reset
# Reset PostgreSQL password
# Switch to postgres user (Linux)
sudo -u postgres psql
# Change postgres user password
ALTER USER postgres WITH PASSWORD 'newpassword';
\q
# Or use pg_hba.conf trust method temporarily
sudo nano /etc/postgresql/14/main/pg_hba.conf
# Change this line:
# local all postgres peer
# To:
# local all postgres trust
# Restart PostgreSQL
sudo systemctl restart postgresql
# Connect without password
psql -U postgres
# Change password
ALTER USER postgres WITH PASSWORD 'newpassword';
# Revert pg_hba.conf back to peer/md5
Error: Port 5432 already in use
Another PostgreSQL instance or application is using port 5432.
- Check for multiple PostgreSQL installations
- Another database server on port 5432
- Previous PostgreSQL instance not shut down
# Find what's using port 5432
# Windows
netstat -ano | findstr :5432
tasklist | findstr [PID]
# Linux/Mac
sudo lsof -i :5432
sudo netstat -tulpn | grep :5432
# Kill process
sudo kill -9 [PID]
# Stop PostgreSQL service
sudo systemctl stop postgresql
brew services stop postgresql
PostgreSQL Configuration Files
| System |
postgresql.conf Location |
| Ubuntu/Debian |
/etc/postgresql/14/main/postgresql.conf |
| CentOS/RHEL |
/var/lib/pgsql/14/data/postgresql.conf |
| Mac (Homebrew) |
/usr/local/var/postgres/postgresql.conf |
| Windows |
C:\Program Files\PostgreSQL\14\data\postgresql.conf |
Find Config File Location
# Connect to PostgreSQL
psql -U postgres
# Show config file location
SHOW config_file;
# Show data directory
SHOW data_directory;
# Show hba file location
SHOW hba_file;
# Exit
\q
Change PostgreSQL Port from 5432
# Edit postgresql.conf
sudo nano /etc/postgresql/14/main/postgresql.conf
# Find and change port
port = 5433
# Save and restart PostgreSQL
sudo systemctl restart postgresql
# Connect to new port
psql -h localhost -p 5433 -U postgres
# Update application connection strings to use port 5433
Allow Remote Connections
By default, PostgreSQL only accepts connections from localhost.
Edit postgresql.conf
# Edit postgresql.conf
sudo nano /etc/postgresql/14/main/postgresql.conf
# Change listen_addresses
listen_addresses = '*' # Listen on all interfaces
# Or specific IP
listen_addresses = '192.168.1.100,localhost'
# Save and restart
sudo systemctl restart postgresql
Edit pg_hba.conf
# Edit pg_hba.conf
sudo nano /etc/postgresql/14/main/pg_hba.conf
# Add line to allow remote connections
# Allow from specific IP
host all all 192.168.1.0/24 md5
# Allow from any IP (not recommended for production)
host all all 0.0.0.0/0 md5
# Save and reload configuration
sudo systemctl reload postgresql
# Test remote connection
psql -h 192.168.1.100 -p 5432 -U postgres -d mydb
Common PostgreSQL Commands
# Connect to PostgreSQL
psql -U postgres
# List all databases
\l
# Connect to database
\c database_name
# List all tables in current database
\dt
# Describe table structure
\d table_name
# List all users/roles
\du
# Show current database
SELECT current_database();
# Show current user
SELECT current_user;
# Execute SQL file
\i /path/to/file.sql
# Export query results to CSV
\copy (SELECT * FROM users) TO '/tmp/users.csv' CSV HEADER
# Get help
\?
# Quit
\q
Create Database and User
# Connect as postgres
psql -U postgres
# Create new database
CREATE DATABASE myapp;
# Create new user
CREATE USER myuser WITH PASSWORD 'mypassword';
# Grant privileges
GRANT ALL PRIVILEGES ON DATABASE myapp TO myuser;
# Connect to database
\c myapp
# Grant table privileges
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO myuser;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO myuser;
# Exit
\q
# Test connection with new user
psql -U myuser -d myapp -h localhost
Configure Firewall for Port 5432
Linux Firewall (UFW)
# Allow PostgreSQL port
sudo ufw allow 5432/tcp
# Allow from specific IP only
sudo ufw allow from 192.168.1.0/24 to any port 5432
# Check status
sudo ufw status numbered
# Remove rule
sudo ufw delete allow 5432/tcp
Linux Firewall (firewalld)
# Allow PostgreSQL service
sudo firewall-cmd --permanent --add-service=postgresql
sudo firewall-cmd --reload
# Or allow port directly
sudo firewall-cmd --permanent --add-port=5432/tcp
sudo firewall-cmd --reload
# Check open ports
sudo firewall-cmd --list-all
PostgreSQL GUI Tools
- pgAdmin 4 - Official PostgreSQL administration tool
- DBeaver - Universal database tool
- DataGrip - JetBrains database IDE
- TablePlus - Modern database client
- Postico - PostgreSQL client for Mac
- HeidiSQL - Database client for Windows
- Adminer - Web-based database management
PostgreSQL vs MySQL on Port Comparison
| Feature |
PostgreSQL (5432) |
MySQL (3306) |
| Default Port |
5432 |
3306 |
| ACID Compliance |
Full |
InnoDB only |
| JSON Support |
JSONB (binary) |
JSON (text) |
| Replication |
Streaming, logical |
Master-slave, Group |
| License |
PostgreSQL (MIT-like) |
GPL / Commercial |
| Full Text Search |
Built-in |
Basic |
Security Warning:
Never expose PostgreSQL port 5432 directly to the internet. Always use VPN, SSH tunneling, or restrict access by IP in pg_hba.conf. Use strong passwords and keep PostgreSQL updated.
Frequently Asked Questions
Can I access PostgreSQL port 5432 in browser?
No. Port 5432 is for PostgreSQL protocol, not HTTP. Use pgAdmin or other GUI tools to manage databases through web interface.
Why can't I connect to PostgreSQL on port 5432?
Common reasons: PostgreSQL not running, wrong credentials, pg_hba.conf blocking connections, firewall blocking port, or PostgreSQL bound to 127.0.0.1 only.
What's the difference between peer and md5 authentication?
Peer uses OS username (no password needed), md5 requires password. Peer works for local connections only. md5 works for both local and remote connections.
How do I backup PostgreSQL database?
Use pg_dump: pg_dump -U postgres database_name > backup.sql. Restore with: psql -U postgres database_name < backup.sql
Can I run PostgreSQL on different port?
Yes. Edit postgresql.conf and change port value. Remember to update all application connection strings to use new port.
Related Ports and Resources