How to Start and Troubleshoot PostgreSQL Server on Ubuntu: Fixing “psql: could not connect to server” Error


2 views

First, let's check if the PostgreSQL service is running:

sudo systemctl status postgresql

If the service isn't active, you'll see output indicating it's inactive or failed. A healthy service should show "active (running)".

To start the PostgreSQL server:

sudo systemctl start postgresql

For Ubuntu systems using older init systems (pre-15.04):

sudo service postgresql start

PostgreSQL by default listens on localhost (127.0.0.1) port 5432. Verify this with:

sudo -u postgres psql -c "SHOW hba_file;"
sudo -u postgres psql -c "SHOW listen_addresses;"

The error suggests PostgreSQL isn't creating the socket file. Check the socket directory:

ls -la /var/run/postgresql/

If missing, create it and set proper permissions:

sudo mkdir -p /var/run/postgresql
sudo chown postgres:postgres /var/run/postgresql

Edit postgresql.conf (location varies by version, typically /etc/postgresql/8.3/main/):

sudo nano /etc/postgresql/8.3/main/postgresql.conf

Ensure these lines exist (uncommented):

listen_addresses = 'localhost'
unix_socket_directory = '/var/run/postgresql'

After getting the server running, set a password for postgres user:

sudo -u postgres psql
ALTER USER postgres PASSWORD 'your_password';

1. Port conflict - check if another service uses 5432:

sudo netstat -tulnp | grep 5432

2. Data directory permissions:

sudo chown -R postgres:postgres /var/lib/postgresql

3. Corrupted installation - reinstall if needed:

sudo apt-get --purge remove postgresql
sudo apt-get install postgresql

When working with PostgreSQL on Ubuntu, a common error new users encounter is:

psql: could not connect to server: No such file or directory
Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

This typically indicates one of three scenarios:

  • The PostgreSQL service isn't running
  • It's running but not configured for local connections
  • The socket file is in a non-standard location

First, verify if the service is running:

sudo systemctl status postgresql

If it's not running, start it with:

sudo systemctl start postgresql

To enable automatic startup on boot:

sudo systemctl enable postgresql

The initial command in the question had a small typo ("sudu" instead of "sudo"). Here's the correct version:

sudo -u postgres psql template1

Once connected, you can set a password for the postgres user:

ALTER USER postgres WITH PASSWORD 'your_password';

If the service is running but you still can't connect, check these files:

1. pg_hba.conf (usually in /etc/postgresql/8.3/main/):

# TYPE  DATABASE    USER        ADDRESS          METHOD
local   all         all                         peer
host    all         all         127.0.0.1/32     md5

2. postgresql.conf (same directory):

listen_addresses = 'localhost'

If you're still facing issues:

# Check if postgres is listening
sudo netstat -plunt | grep postgres

# Alternative way to start the service
sudo service postgresql restart

# Check log files
sudo tail -n 50 /var/log/postgresql/postgresql-8.3-main.log

Note that PostgreSQL 8.3 is quite old (released 2007). For production systems, consider upgrading. Here's how to install a newer version:

# Add the official PostgreSQL repository
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-14