'PostgreSQL Connection Refused

I have installed PostgreSQL. However everytime I try to connect through PGAdmin or through psql it gives me the below error.

could not connect to server: Connection refused (0x0000274D/10061) 
      Is  the server running on host "localhost" (::1) and 
      accepting TCP/IP  connections on port 5432? 
could not connect to server: Connection  refused (0x0000274D/10061) 
      Is the server running on host "localhost" (127.0.0.1) and 
      accepting TCP/IP connections on port 5432?

I checked the postgresql.conf file and the line: listen_addresses = '*' isn't commented out.

Also this is how my pg_hba.conf file is set:

# IPv4 local connections:
  host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
  host    all             all             ::1/128                 trust

Can anyone please advise on how I can resolve this issue?

Thanks!



Solution 1:[1]

Use services "start -> run -> services.msc" and look for the postgresql-[vers] service.

If it is not running try to start it, if it won't start open the event-viewer (start -> run -> eventvwr) and look for error messages relating to the PostgreSQL service.

Solution 2:[2]

pg_hba.conf.txt has to be called pg_hba.conf.

Beyond this, when you have en authentication error you will get a message similar to:

psql -U nonexistent
psql: FATAL:  Peer authentication failed for user "nonexistent"

The error you are getting means most likely that PostgreSQL is not started on this server.

You can start PostgreSQL with:

service postgresql start
service postgresql status

Solution 3:[3]

This worked for me -

In C:\Program Files\PostgreSQL\data\postgresql.conf set listen_addresses ='localhost'

Then try -

pg_ctl -D "C:\Program Files\PostgreSQL\9.5\data" start

if already try restarting using pg_ctl

Solution 4:[4]

It works for me. Remember, whatever comes after -D should be the path to where you installed PostgreSQL, to the data folder, which holds the pg_hba.conf and postgresql.conf files.

pg_ctl start -D "C:/Program Files/PostgreSQL/9.6/data"

Solution 5:[5]

Maybe you can try this. Open cmd and insert.

"C:\Program Files\PostgreSQL\11\bin\pg_ctl.exe" runservice -N "postgresql-x64-11" -D "C:\Program Files\PostgreSQL\11\data" -w

Hit enter...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 tayfun K?l?รง
Solution 2 Gab
Solution 3 Karthik Sagar
Solution 4 Kairat Koibagarov
Solution 5 Dharman