11

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!

MwamiTovi
  • 2,425
  • 17
  • 25
dino9239
  • 111
  • 1
  • 1
  • 3

6 Answers6

7

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.

tayfun Kılıç
  • 2,042
  • 1
  • 14
  • 11
  • 1
    After searching for several answers, this one worked for me! It simply needed to start the application (right click -> start). Then the error is gone! – Albert Israel Nov 29 '18 at 13:51
1

In my case it was postgres.config. The port was somehow changed to 5433 in there and I do not remember doing it myself lol. So make sure your ports are matching up in your project vs postgres.config

Chris R
  • 29
  • 2
  • This may have happened if you installed a second version of PostgreSQL while another was already installed (maybe while trying to migrate from version X to version Y), as the second version will see that there is already another PostgreSQL installed and using port 5432, so will automatically set its port to 5433 in `postgres.conf` so that it doesn't conflict with the other PostgreSQL when both servers are running. – Nick Muise Dec 21 '22 at 17:50
0

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
Gab
  • 3,404
  • 1
  • 11
  • 22
  • 1
    `psql -U myuser -d mydatabase` works fine, I did start the service and `status`shows it as *active*. But I still get that `Connection refused` error. – DarksteelPenguin Oct 16 '17 at 14:49
0

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

Karthik Sagar
  • 424
  • 4
  • 7
0

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...

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

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"
Kairat Koibagarov
  • 1,385
  • 15
  • 9