You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法连接PostgreSQL服务器,报错Connection refused如何解决?

Fixing Django-PostgreSQL Connection Refused Error (10061)

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, type services.msc, and hit Enter. Look for the PostgreSQL service (it'll have a name like postgresql-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 (replace 14 with 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_addresses line, and set it to:
    listen_addresses = '127.0.0.1'
    
    (You can also use '*' to allow connections from all addresses, but 127.0.0.1 is 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-256 is default for newer versions, md5 for 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:38:26