The Error Message
You try to connect to your database, but instead of a password prompt, you're hit with this blunt message:
psql: error: could not connect to server: could not translate host name "db.example.com" to address: Name or service not known
PostgreSQL isn't actually the culprit here. This error comes from your operating system's network stack. It means your system tried to find the IP address for db.example.com and came back empty-handed. If the OS can't map the name to an IP, the connection dies before it even starts.
What’s Usually Breaking?
DNS (Domain Name System) is the phonebook of the internet. When you give psql a hostname, the OS has to convert it into a numeric IP address, like 10.0.5.42. If that lookup fails, the handshake never happens.
Look for these common triggers:
- Simple Typos: A misspelled hostname in your
.envfile or connection string. - Unreachable DNS: Your configured nameserver (like
8.8.8.8) is down or blocked by a firewall. - VPN/VPC Isolation: You're trying to reach a private AWS RDS instance without being connected to the right VPC.
- Missing Local Maps: The hostname is a custom alias that hasn't been added to
/etc/hosts. - Docker Isolation: Your app container and database container aren't sharing a network.
Step-by-Step Fixes
1. Rule Out Typos
It sounds obvious, but a single misplaced character will kill the connection. Check for extra spaces or missing dots in your host variables.
# Incorrect - missing a letter
psql -h db.postgre.com -U postgres
# Correct
psql -h db.postgres.com -U postgres
2. Test the Connection Manually
Don't guess; verify what your OS sees. Use standard networking tools to see if the host exists at all. Run these from your terminal:
# Check for a basic response
ping db.example.com
# Ask your DNS server directly
nslookup db.example.com
# Get the full DNS trace (best for debugging)
dig db.example.com
If these return "NXDOMAIN" or "Name or service not known", your DNS configuration is the problem, not PostgreSQL.
3. Force a Local Map in /etc/hosts
If you're working on a local dev server without a public DNS record, you need to map it manually. This bypasses DNS servers entirely.
On Linux or macOS, open the hosts file:
sudo nano /etc/hosts
Add the IP and hostname at the bottom. For example, if your server is at 192.168.1.105:
192.168.1.105 db.example.com
Save it and try connecting again. This is a great quick fix, but don't rely on it for production scaling.
4. Repair Your DNS Resolver
If your server can't resolve any external websites, your resolv.conf might be empty or corrupted.
cat /etc/resolv.conf
If it's missing nameservers, you can temporarily add reliable ones like Google or Cloudflare to get back online:
nameserver 8.8.8.8
nameserver 1.1.1.1
5. Docker and Container Networking
In Docker, containers usually resolve each other by their service name. If your app container can't see the db container, they probably aren't on the same virtual network.
# Example docker-compose.yml snippet
services:
db:
image: postgres
networks:
- backend
app:
networks:
- backend
environment:
- DATABASE_URL=postgres://user:pass@db:5432/dbname # 'db' is the hostname
Ensure both services share the same network block in your compose file.
Verifying the Fix
Once you've adjusted your settings, use psql with the -h flag to confirm the connection is live:
psql -h db.example.com -U your_username -d your_database -W
If you see the Password for user ...: prompt or the PostgreSQL shell (=>), you've cleared the DNS hurdle.
Prevention and Best Practices
Hostnames are safer than hardcoded IPs, but they require a stable foundation. Keep your network resilient with these tips:
- Use Managed DNS: For production, use AWS Route53 private zones or Cloudflare instead of brittle
/etc/hostsentries. - Service Discovery: In Kubernetes, use internal service names (e.g.,
db.default.svc.cluster.local) to ensure stable addressing even if pods restart. - Plan Your Subnets: Proper network planning prevents routing loops. I use an IP Subnet Calculator to map out CIDR ranges accurately. This ensures your DNS servers and database nodes sit in reachable segments, avoiding "address not known" errors caused by isolated subnets.
- TTL Awareness: If you recently moved your database, remember that DNS records can take 5 to 60 minutes to propagate depending on your TTL settings.

