Psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory

Hi,

I have installed DHIS2 lxc containers on my laptop, and Im having some problems to access postgres PSQL. Thaings were working well, but suddenly I starting getting the following error: psql: error: connection to server on socket “/var/run/postgresql/.s.PGSQL.5432” failed: No such file or directory

  1. The first time it worked
    root@postgres:~# sudo -u postgres psql
    psql (16.8 (Ubuntu 16.8-1.pgdg22.04+1))
    Type “help” for help.

postgres=# \du
List of roles
Role name | Attributes
-----------±-----------------------------------------------------------
dhis |
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS
root | Superuser

  1. Now Iam getting the error:

  2. This is the postgresql configuration file

Looking forward to a solution

Thanks

PostgreSQL Server Troubleshooting Guide

This guide walks you through the process of checking if your PostgreSQL server is running and how to restart it if needed.

Checking if PostgreSQL Server is Running

1. Check Network Connections

First, check if PostgreSQL is listening on its network socket by examining open connections:

ss -ntaup | grep postgres

This command shows all TCP connections and listening ports, filtering for PostgreSQL-related processes. You should see entries with port 5432 (PostgreSQL’s default port) if the server is running correctly.

2. Check PostgreSQL Cluster Status

For PostgreSQL installed via Debian/Ubuntu packages, you can check the status of your specific cluster:

pg_ctlcluster 16 main status

The output will tell you whether the cluster is running or stopped. Replace 16 with your PostgreSQL version if different.

3. Verify Database Connectivity

The pg_isready utility provides a simple way to check if the PostgreSQL server is accepting connections:

sudo -u postgres /usr/lib/postgresql/16/bin/pg_isready

If the server is running and ready to accept connections, you’ll see output like:

/var/run/postgresql:5432 - accepting connections

Restarting PostgreSQL Server

If your PostgreSQL server isn’t running or is experiencing issues, you can restart it using one of these methods:

Method 1: Using pg_ctlcluster (for Debian/Ubuntu package installations)

sudo pg_ctlcluster 16 main restart

Method 2: Using systemd (modern Linux distributions)

sudo systemctl restart postgresql

or for version-specific service:

sudo systemctl restart postgresql@16-main

Method 3: Restarting PostgreSQL in Docker

If you’re running PostgreSQL in a lxc container:

Then restart the container


## Troubleshooting Common Issues

If PostgreSQL still won't start after attempting to restart:

1. Check the PostgreSQL logs:
   ```bash
   sudo tail -n 100 /var/log/postgresql/postgresql-16-main.log
  1. Verify PostgreSQL data directory permissions:

    sudo ls -la /var/lib/postgresql/16/main
    
  2. Ensure you have sufficient disk space:

    df -h
    
1 Like

Try to active postgres service with lxc exec postgres – systemctl start postgresql