Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.3k views
in Technique[技术] by (71.8m points)

database - PostgreSQL: could not connect to server - Connection refused error

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...