When your PostgreSQL server suddenly refuses to start with the dreaded "could not create any TCP/IP sockets" error, the root cause often lies in how your system resolves localhost
. Here's what happened in this specific case:
2014-06-11 10:32:41 CEST LOG: could not bind IPv4 socket: Cannot assign requested address
2014-06-11 10:32:41 CEST HINT: Is another postmaster already running on port 5432?
2014-06-11 10:32:41 CEST WARNING: could not create listen socket for "localhost"
2014-06-11 10:32:41 CEST FATAL: could not create any TCP/IP sockets
The key observation was that localhost
was resolving to 217.74.65.145
instead of the expected 127.0.0.1
. This immediately suggests DNS or hosts file misconfiguration.
Checking the /etc/hosts
file revealed:
127.0.0.1 local
127.0.1.1 jacek-X501A1
127.0.0.1 something.name.non.example.com
127.0.0.1 company.something.name.non.example.com
Notice the missing standard localhost
entry that should map to 127.0.0.1
.
The immediate solution was to modify postgresql.conf
:
# Change from:
# listen_addresses = 'localhost'
# To:
listen_addresses = '127.0.0.1'
And update the Rails database configuration:
# config/database.yml
development:
host: 127.0.0.1
port: 5432
# ... other settings
While changing to 127.0.0.1 works, the proper fix is to correct the hosts file. Edit /etc/hosts
to include:
127.0.0.1 localhost
::1 localhost
After making these changes, flush your DNS cache (method varies by OS) and verify with:
ping localhost
# Should show 127.0.0.1
dig localhost
# Should return 127.0.0.1
PostgreSQL's socket binding behavior is particular about IP addresses. When configured to listen on localhost
, it needs this to properly resolve to a valid local IP. The error occurs because:
- PostgreSQL tries to bind to the resolved IP of "localhost"
- If that IP isn't available on your system (like the public 217.74.65.145), binding fails
- The server then fails to start entirely
For thorough troubleshooting, check port 5432 status before restarting PostgreSQL:
# Linux/MacOS
sudo lsof -i :5432
netstat -tulnp | grep 5432
# Windows
netstat -ano | findstr 5432
If another process is using the port, you'll need to either stop that process or configure PostgreSQL to use a different port.
For development environments, I recommend these PostgreSQL settings:
# postgresql.conf
listen_addresses = '127.0.0.1' # More reliable than 'localhost'
port = 5432 # Standard port, change if conflicts exist
max_connections = 100 # Development typically needs fewer
Remember to restart PostgreSQL after configuration changes:
sudo service postgresql restart
# Or on systemd systems:
sudo systemctl restart postgresql
If you're still having connection issues, test basic connectivity:
# Test if PostgreSQL is accepting connections
telnet 127.0.0.1 5432
# Check PostgreSQL status
sudo -u postgres psql -c "SELECT version();"
For more complex environments (like Docker), you may need additional network configuration, but that's beyond our current scope.
When PostgreSQL fails with "could not create any TCP/IP sockets", it typically indicates network stack configuration problems. The error message reveals two critical components failing:
2014-06-11 10:32:41 CEST LOG: could not bind IPv4 socket: Cannot assign requested address
2014-06-11 10:32:41 CEST WARNING: could not create listen socket for "localhost"
2014-06-11 10:32:41 CEST FATAL: could not create any TCP/IP sockets
The surprising discovery was that localhost
resolved to 217.74.65.145
instead of the expected 127.0.0.1
. This indicates DNS resolution issues in the system configuration. Checking /etc/hosts
revealed:
127.0.0.1 local
127.0.1.1 jacek-X501A1
127.0.0.1 something.name.non.example.com
127.0.0.1 company.something.name.non.example.com
Notice the missing standard localhost
entry that most systems expect. The hostname resolution was falling back to DNS lookup due to this omission.
The working solution involved modifying postgresql.conf
to explicitly use 127.0.0.1
:
listen_addresses = '127.0.0.1' # instead of 'localhost'
port = 5432 # default port, change if needed
Corresponding database configuration changes were required in the Rails application:
# config/database.yml
development:
host: 127.0.0.1
port: 5432
username: postgres
password: your_password
database: app_development
To prevent future issues, edit /etc/hosts
to include proper localhost mapping:
# /etc/hosts
127.0.0.1 localhost
::1 localhost
127.0.0.1 local
127.0.1.1 jacek-X501A1
If problems persist, consider these diagnostic commands:
# Check port availability
sudo netstat -tulnp | grep 5432
# Verify PostgreSQL status
sudo systemctl status postgresql
# Check connection attempts
sudo tail -f /var/log/postgresql/postgresql-9.3-main.log
# Test raw connection
psql -h 127.0.0.1 -p 5432 -U postgres
For development environments not requiring TCP/IP, configure PostgreSQL to use Unix sockets:
# postgresql.conf
listen_addresses = '' # empty means no TCP/IP
unix_socket_directories = '/var/run/postgresql'
Then modify your Rails connection:
development:
host: /var/run/postgresql
port: