PostgreSQL psql: could not connect to server: Connection refused

Q. When I try to connect remote PostgreSQL, I am reciving an error which read as follows:

psql: could not connect to server: Connection refused
Is the server running on host and accepting
TCP/IP connections on port 5432?

How do I fix this problem? I am using CentOS 4.x version.

A. First make sure PostgreSQL server has been started to remote server.

# /etc/init.d/postgresql start

If it is running and you get above error, you need to add enable TCP/IP support. By default, the PostgreSQL server only allows connections to the database from the local machine or localhost. This is a security feature.

Step # 1: Allow remote IP address to access PostgreSQL

You need to open file called /var/lib/pgsql/data/pg_hba.conf. Login as postgres user using su command:
$ su - postgres
$ vi /var/lib/pgsql/data/pg_hba.conf

Now append following line. Let us say you would like to give access to network:
host all all trust
Please replace and to reflect the actual network IP address range of the clients system in your own network.

Save close the file.

Step # 2: Allow communication over TCP/IP

You need to open PostgreSQL configuration file /var/lib/pgsql/data/postgresql.conf
$ vi /var/lib/pgsql/data/postgresql.conf
Now bind and open TCP/IP port by setting tcpip_socket to true:
tcpip_socket = true

Save and close the file.

Step # 3: Restart PostgreSQL server

Restart the PostgreSQL server with the following command
# /etc/init.d/postgresql restart

This will open default port 5432.

Step # 4: Test your setup

Use psql command from client system as follows:

Connect to remote server by IP address and login using vivek user to connect to sales database, use:
$ psql -h -U vivek -d sales

  • -h : Specifies the host name of the machine or IP address ( on which the server is running.
  • -U vivek : Connect to the database as the vivek username instead of the default. You must have account and permission to connect as vivek user.
  • -d sales : Specifies the name of the database (sales) to connect to.

See also:

=> How do I add a user to PostgreSQL database server?

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 21 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
21 comments… add one
  • Kamchybek Jusupov Mar 23, 2007 @ 10:48

    $ su – postgres
    $ vi /var/lib/pgsql/data/pg_hba.conf

    You can also use instead of (CIDR-address), kinda config file will look “cleaner”, and I would use “md5” instead of “trust” for further peace-of-mind :)

  • Rajeev Jan 9, 2008 @ 8:14

    Thanks for the fantastic FAQ. Just what I needed as a newbie with pgsql.

    Just wanted to mention here that was trying to connect a remote pgsql db on a Suse Linux 9. It did not have the tcpip_socket parameter in postgresql.conf

    To overcome this, I had to uncomment the following settings:


    # – Connection Settings –

    listen_addresses = ‘*’
    port = 5432

    Bounced the db and voila, I was able to connect and to run queries.

    Hope this helps.


  • Rajeev Jan 9, 2008 @ 8:16

    Forgot to mention the version of my postgresql db. It’s 8.1.4

  • deven Jan 31, 2008 @ 5:51

    this guide was a good help for me.
    only thing as rajeev said their is no parameter with the name tcpip_socket in Ubuntu also.

    so used following things in the postgresql.conf config file

    listen_addresses = ‘*’
    port = 5432

  • quocvietit Aug 22, 2008 @ 15:34

    please help me.i try to connect to Postgresql sever by LAN
    i already configured following step but not success
    here my config on flatform Window
    host all all md5
    i try all
    #listen_addresses = ‘*’
    port = 5432


    tcpip_socket = true

    it’s still not working
    thanks anyway!

  • GILBERT Oct 23, 2009 @ 13:53

    I Remote PostgreSQL Access On the web

    how can i Connect to my postgresql database on my server remotely through the web/internet and manage it directly from your PC?and even connect some application so i run transaction?

  • Scotos Feb 4, 2010 @ 8:24

    I have the same problem as quocvietit. I’m also in Windows.

  • Raveendar Mar 1, 2010 @ 5:10

    Can anybody help me with this…

    I’ve postgre db on redhat linux. It’s been configured to a single network earlier viz., but now I want to access the same db from different vlans( ,……to there are about 20 machines on each network. I’m able to access from the client machines which are on… network but where as when I try to access from others it says “I don’t permissions to access the db”. Pls can anybody help in this regard…

  • Raveendar Mar 1, 2010 @ 5:15

    I opened this file but
    $ vi /var/lib/pgsql/data/pg_hba.conf but couldnot find
    host all… instead if find
    host = ” ”

    how can I solve this

  • martin Feb 3, 2011 @ 6:35

    user account postgres does exist but i can’t login to it hence postgres service does not do i go about this?

  • koko100mohaed Apr 19, 2011 @ 10:10

    hiii mohad mekhelfi ana algerie b/amer (algerie )

  • Black Teppi Jul 19, 2011 @ 4:06

    I have same problem.
    I use Windows and PostgreSql 9.0.4
    anybody help?
    So thanks!

    • ErpBeginner Aug 1, 2011 @ 0:43

      Hi everyone , i’m havin’ same problem , Postgres 8.4 server does start , but when i try to run OpenErp Client , an error appear in the db field : Server couldnt start !! Could Anybody help me ppl ….Configuration : win7 32bits ,

      • mansoor Oct 23, 2015 @ 15:35

        Could’not connection to the server

  • Pomodoro Aug 3, 2011 @ 10:17

    And how about this error in postgres 9.0?
    FATAL: unrecognized configuration parameter “tcpip_socket”

  • biju Aug 23, 2012 @ 6:25

    while installing postgres sql 8.0 in windows an error occurs ‘port number already in use please specify another ‘ how can i solve this

  • paulo roberto Oct 8, 2012 @ 18:58

    meu banco de dados postgresql não esta abrindo ele manda msg dizendo que não consegue se comunicar com o servidor ,estou usando o servidor virtual apache ,o que faço para não perder os meus dados ?, já até reinstalei o apache mais não resolve,o sistema é windows 7

  • Puneet Pandey Aug 14, 2013 @ 9:11

    What a wonderful post and the tremendous response from Rajeev. It took me hardly 3 mins to configure this and access PG remotely.

  • Bob Gustafson Mar 31, 2016 @ 19:37

    I am running on Fedora23 with postgresql-9.4.6-1.fc23.x86_64

    I have adjusted the postgresql.conf and pg_hba.conf and the firewall so the server listens on my local network.

    When I reboot the server machine (sudo shutdown -r now), I am initially not able to connect to the postgresql server. However, if I restart the server:

    systemctl restart postgresql.conf

    Then I am able to connect to the server from a machine on the network.

    A puzzle.

    • Bob Gustafson Apr 1, 2016 @ 14:31

      The restart command in the previous comment is really:

      systemctl restart postgresql.service

      Thanks for any comments to this puzzle.

  • Ammar Haider Aug 4, 2020 @ 13:48

    /var/lib/postgresql//main/postgresql.conf didn’t work for me.
    instead found the postgresql.conf and hg_pba.conf in /etc directory and remote access worked pretty fine.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum