无法连接PostgreSQL服务器,报错Connection refused如何解决?
Hey there, let's work through this connection issue you're hitting. The error message makes it clear your Django app can't reach the PostgreSQL server at 127.0.0.1:5432—here are the step-by-step fixes to get things working again:
1. Make sure the PostgreSQL service is running
First things first: check if PostgreSQL is even up and running on your machine.
- On Windows, press
Win + R, typeservices.msc, and hit Enter. Look for the PostgreSQL service (it'll have a name likepostgresql-x64-<your-version>). If its status isn't "Running", right-click it and select Start. - Alternatively, use Command Prompt: run
net start postgresql-x64-14(replace14with your PostgreSQL version number).
2. Verify PostgreSQL is listening on port 5432
Next, confirm the server is actually accepting connections on port 5432:
- Open Command Prompt and run:
netstat -ano | findstr :5432 - If you see a line with
LISTENING, the port is active. If not, we'll need to adjust PostgreSQL's config.
3. Update PostgreSQL's listening address
If the port isn't listening, edit the postgresql.conf file to allow local connections:
- Find the file in
C:\Program Files\PostgreSQL\<your-version>\data. - Open it with a text editor, locate the
listen_addressesline, and set it to:
(You can also uselisten_addresses = '127.0.0.1''*'to allow connections from all addresses, but127.0.0.1is safer for local setups.) - Save the file and restart the PostgreSQL service from the Services window.
4. Allow local connections in pg_hba.conf
Even if the server is listening, PostgreSQL might block local connections. Check the pg_hba.conf file (also in the data directory):
- Add or ensure this line exists (adjust the authentication method if needed—
scram-sha-256is default for newer versions,md5for older ones):host all all 127.0.0.1/32 scram-sha-256 - Save the file and restart PostgreSQL again.
5. Double-check Django's database configuration
Make sure your Django settings match PostgreSQL's setup. Open your project's settings.py and verify the DATABASES section:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'your_database_name', # Must match the DB you created in PostgreSQL 'USER': 'your_postgres_username', # Usually 'postgres' by default 'PASSWORD': 'your_postgres_password', 'HOST': '127.0.0.1', 'PORT': '5432', } }
Double-check that all values are correct—especially the database name and credentials.
6. Check Windows Firewall settings
Sometimes the firewall blocks PostgreSQL's port. Try temporarily disabling Windows Firewall to test if that's the issue. If the connection works after disabling it:
- Go to Windows Defender Firewall > Advanced Settings > Inbound Rules.
- Create a new rule to allow traffic on port 5432, or allow the PostgreSQL executable (found in
C:\Program Files\PostgreSQL\<version>\bin\postgres.exe).
If none of these steps work, check PostgreSQL's log files in C:\Program Files\PostgreSQL\<version>\data\pg_log—they'll have detailed error messages that can point to the root cause.
内容的提问来源于stack exchange,提问作者Lohith

