6

I've failed to set up postgreSQL to work with my Ruby-on-Rails project for the past week.
I've tried to uninstall and reinstall, postgreSQL, twice now.
But when I try to launch postgreSQL I keep getting the error below:

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've looked at many online resources, including stackoverflow and none seem helpful.
The key parts of my pg_hba.conf file looks like this:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#host    replication     postgres        127.0.0.1/32            md5
#host    replication     postgres        ::1/128                 md5

And the key part of my postgresql.conf file is as follows:

listen_addresses = '*'      # what IP address(es) to listen on;
                    # comma-separated list of addresses;
                    # defaults to 'localhost'; use '*' for all
                    # (change requires restart)
port = 5432             # (change requires restart)
max_connections = 100           # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart)
#unix_socket_directories = ''   # comma-separated list of directories
                    # (change requires restart)
#unix_socket_group = ''         # (change requires restart)
#unix_socket_permissions = 0777     # begin with 0 to use octal notation

Most of the suggestions, I've seen so far, were based on those two files. (For my case, they were already configured correctly). I also tried disabling the firewall and restarting postgreSQL but it didn't help. Does anyone have any suggestions for me? Thanks!

18
  • are you on the same pc as the db server? If not you need setup firewall Commented Dec 13, 2016 at 15:53
  • Yes, I'm using the same PC as the db server. How do I setup firewall? I've tried disabling it as well as enabling the postgres app on windows firewall. Commented Dec 13, 2016 at 16:11
  • Firewall only is an issue if you are in a different pc. Can you connect using pgAdmin? Check the service is up and you are using the right port Commented Dec 13, 2016 at 16:13
  • I tried connecting using pgAdmin, it was running the same error as above. The port # is correct an matches across all files. Commented Dec 13, 2016 at 16:38
  • check the services to see if is running. First install should be very easy. I suggest you try another pc. Commented Dec 13, 2016 at 16:46

1 Answer 1

8

Got the same issue while settings up PostgreSQL 9.6.16 to work with Python/Django, but this is purely a database issue.

The solution lies in the error: In fact, I found this error mentioned within the official PostgreSQL documentation thus it's a common error.

And here is how I resolved this issue:

  1. Always first start the postgres database server, use postgres or the wrapper program pg_ctl.
    I used the command, below, on windows 10.
    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"
  1. If that runs well, you are ready to access the database server.
    Open another cmd shell, and type the command below.
    Remember the password you entered while installing PostgreSQL?
    Enter that password when asked Password for user postgres:
> psql -U postgres

Once done, you can now go ahead to CREATE ROLE and CREATE DATABASE accordingly.

Sign up to request clarification or add additional context in comments.

3 Comments

where to run these commands? SQL shell? Windows CMD? what is "wrapper program pg_ctl" ??
Hi, @nishi. Yes, the sign > means you comfortably run this in windows cmd. And true, pg_ctl is a wrapper/utility program to help you run a couple of tasks on the PostgreSQL server. Here's a good read on that
thanks @MwamiTovi. well, the problem when I was trying to run this command was cuz not always the psql command is set in the windows environment path. I solved the issue by editing the postgres server. Mine was on port 5433 and i my db was sending info on 5432 if it helps any further readers. but i still cant run pg_ctl on my cmd.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.