Facing configuration problem with the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?

connection to server at "127.0.0.1", port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
Request Method: 
Django Version: 3.2.4
Exception Type: OperationalError
Exception Value:    
connection to server at "127.0.0.1", port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
Exception Location: /home/ebong/djangoproject/djangoprojectenv/lib/python3.10/site-packages/psycopg2/__init__.py, line 122, in connect
Python Executable:  /home/ebong/djangoproject/djangoprojectenv/bin/python
Python Version: 3.10.12
Python Path:    
\['/home/ebong/djangoproject/smsdigo',
'/home/ebong/djangoproject/djangoprojectenv/bin',
'/usr/lib/python310.zip',
'/usr/lib/python3.10',
'/usr/lib/python3.10/lib-dynload',
'/home/ebong/djangoproject/djangoprojectenv/lib/python3.10/site-packages'\]
Server time:    Wed, 25 Dec 2024 04:11:09 +0000

This happens when a user tries to login into the website. The website is hosted by digital ocean droplet on django, gunicorn, nginx and postgres. For some reasons, I had reported server, then this problem occured.

I have tried the following below.

  1. Restarted my postgresql using sudo systemctl restart postgresql
  2. I have done changes in postgresql.conf and pg_hba.conf

postgresql.conf file below

    listen_addresses = '*'
data_directory = '/var/lib/postgresql/14/main'      # use data in another directory
                    # (change requires restart)
hba_file = '/etc/postgresql/14/main/pg_hba.conf'    # host-based authentication file
                    # (change requires restart)
ident_file = '/etc/postgresql/14/main/pg_ident.conf'    # ident configuration file
                    # (change requires restart)

# If external_pid_file is not explicitly set, no extra PID file is written.
external_pid_file = '/var/run/postgresql/14-main.pid'           # write an extra PID file
                    # (change requires restart)

#listen_addresses = 'localhost'     
port = 5432             # (change requires restart)
max_connections = 500           # (change requires restart)

unix_socket_directories = '/var/run/postgresql' # comma-separated list of directories


# - SSL -

ssl = on
ssl_cert_file = '/etc/ssl/certs/ssl-cert-snakeoil.pem'
ssl_key_file = '/etc/ssl/private/ssl-cert-snakeoil.key'



#------------------------------------------------------------------------------
# RESOURCE USAGE (except WAL)
#------------------------------------------------------------------------------

# - Memory -

shared_buffers = 2GB            # min 128kB
                    # (change requires restart)
huge_pages = off            # on, off, or try
                    # (change requires restart)
#huge_page_size = 0         # zero for system default
                    # (change requires restart)
#temp_buffers = 8MB         # min 800kB
#max_prepared_transactions = 0      # zero disables the feature
                    # (change requires restart)
# Caution: it is not advisable to set max_prepared_transactions nonzero unless
# you actively intend to use prepared transactions.
work_mem = 4MB              # min 64kB
#hash_mem_multiplier = 1.0      # 1-1000.0 multiplier on hash table work_mem
maintenance_work_mem = 1GB      # min 1MB
#autovacuum_work_mem = -1       # min 1MB, or -1 to use maintenance_work_mem
#logical_decoding_work_mem = 64MB   # min 64kB
#max_stack_depth = 2MB          # min 100kB
#shared_memory_type = mmap      # the default is the first option
                    # supported by the operating system:
                    #   mmap
                    #   sysv
                    #   windows
                    # (change requires restart)
dynamic_shared_memory_type = posix  # the default is the first option
                    # supported by the operating system:
                    #   posix
                    #   sysv
                    #   windows
                    #   mmap
                    # (change requires restart)
#min_dynamic_shared_memory = 0MB    # (change requires restart)

# - Disk -

#temp_file_limit = -1           # limits per-process temp file space
                    # in kilobytes, or -1 for no limit

# - Kernel Resources -

#max_files_per_process = 1000       # min 64
                    # (change requires restart)

# - Cost-Based Vacuum Delay -

#vacuum_cost_delay = 0          # 0-100 milliseconds (0 disables)
#vacuum_cost_page_hit = 1       # 0-10000 credits
#vacuum_cost_page_miss = 2      # 0-10000 credits
#vacuum_cost_page_dirty = 20        # 0-10000 credits
#vacuum_cost_limit = 200        # 1-10000 credits

# - Background Writer -

#bgwriter_delay = 200ms         # 10-10000ms between rounds
#bgwriter_lru_maxpages = 100        # max buffers written/round, 0 disables
#bgwriter_lru_multiplier = 2.0      # 0-10.0 multiplier on buffers scanned/round
#bgwriter_flush_after = 512kB       # measured in pages, 0 disables

# - Asynchronous Behavior -
effective_io_concurrency = 200      # 1-1000; 0 disables prefetching



#------------------------------------------------------------------------------
# WRITE-AHEAD LOG
#------------------------------------------------------------------------------

# - Settings -



# - Checkpoints -

#checkpoint_timeout = 5min      # range 30s-1d
checkpoint_completion_target = 0.9  # checkpoint target duration, 0.0 - 1.0
#checkpoint_flush_after = 256kB     # measured in pages, 0 disables
#checkpoint_warning = 30s       # 0 disables
max_wal_size = 4GB
min_wal_size = 1GB

# - Archiving -

#archive_mode = off     # enables archiving; off, on, or always
                # (change requires restart)
#archive_command = ''       # command to use to archive a logfile segment
                # placeholders: %p = path of file to archive
                #               %f = file name only
                # e.g. 'test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f'
#archive_timeout = 0        # force a logfile segment switch after this
                # number of seconds; 0 disables

# - Archive Recovery -

# These are only used in recovery mode.

#restore_command = ''       # command to use to restore an archived logfile segment
                # placeholders: %p = path of file to restore
                #               %f = file name only
                # e.g. 'cp /mnt/server/archivedir/%f %p'
#archive_cleanup_command = ''   # command to execute at every restartpoint
#recovery_end_command = ''  # command to execute at completion of recovery




#------------------------------------------------------------------------------
# REPLICATION
#------------------------------------------------------------------------------

# - Sending Servers -

# Set these on the primary and on any standby that will send replication data.

#max_wal_senders = 10       # max number of walsender processes
                # (change requires restart)
#max_replication_slots = 10 # max number of replication slots
                # (change requires restart)
#wal_keep_size = 0      # in megabytes; 0 disables
#max_slot_wal_keep_size = -1    # in megabytes; -1 disables
#wal_sender_timeout = 60s   # in milliseconds; 0 disables
#track_commit_timestamp = off   # collect timestamp of transaction commit
                # (change requires restart)


# - Other Planner Options -

default_statistics_target = 100 # range 1-10000

cluster_name = '14/main'            # added to process titles if nonempty
                    # (change requires restart)

# These settings are initialized by initdb, but they can be changed.
lc_messages = 'C.UTF-8'         # locale for system error message
                    # strings
lc_monetary = 'C.UTF-8'         # locale for monetary formatting
lc_numeric = 'C.UTF-8'          # locale for number formatting
lc_time = 'C.UTF-8'             # locale for time formatting

pg_hba.conf file below

# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            scram-sha-256
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host    all             all             ::1/128                 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256

I have tried editing postgres.conf and pg_hba.conf file .

Added 5432 to the firewall .

# I rand sudo ufw status Status: active

To                         Action      From

5432/tcp                   ALLOW       Anywhere 5432                       ALLOW       Anywhere ) 5432/tcp (v6)              ALLOW       Anywhere (v6) 5432 (v6)                  ALLOW       Anywhere (v6)
Вернуться на верх