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

PgAdmin 4及Django连接PostgreSQL失败(错误10061)如何解决?

Hey there! Let's walk through fixing this "connection refused" error step by step—it’s a super common hiccup when setting up PostgreSQL on Windows, so you’re not alone.

1. First: Confirm the PostgreSQL Server is installed and running

A lot of folks mix up PgAdmin 4 with the actual PostgreSQL server—PgAdmin is just a management tool, not the database itself. Here’s how to check:

  • Press Win + R, type services.msc, and hit Enter.
  • Look for a service named something like postgresql-x64-<your-version> (e.g., postgresql-x64-16).
    • If it’s not running: Right-click > Start.
    • To avoid this issue in the future: Set its startup type to Automatic (right-click > Properties > Startup type).
2. Check if port 5432 is occupied by another process

Sometimes other apps (like other databases or even some background tools) can hog port 5432. Let’s verify:

  • Open Command Prompt (CMD) as a regular user, run:
    netstat -ano | findstr :5432
    
  • If you see results:
    • Note the PID number in the last column.
    • Open Task Manager > Details tab, find that PID, and end the process (if it’s not PostgreSQL).
  • If the PID matches the PostgreSQL service, move on to the next step.
3. Make sure PostgreSQL allows TCP/IP connections

By default, PostgreSQL might only accept local socket connections. Let’s tweak the config:

  • Navigate to your PostgreSQL data folder—usually C:\Program Files\PostgreSQL\<version>\data.
  • Edit postgresql.conf with a text editor (run as admin to save changes):
    • Find the line listen_addresses = 'localhost' (it might be commented out with #). Uncomment it and set it to 'localhost' or '*' (to allow connections from other machines too).
  • Edit pg_hba.conf (also in the data folder):
    • Add this line at the end to allow local TCP/IP connections with secure authentication:
      host    all             all             127.0.0.1/32            scram-sha-256
      
      Note: For older PostgreSQL versions, you might use md5 instead of scram-sha-256.
  • Restart the PostgreSQL service (go back to services.msc, right-click > Restart).
4. Allow PostgreSQL through Windows Firewall

Windows Firewall can block incoming connections to port 5432 without you noticing. Here’s how to fix that:

  • Open Windows Defender Firewall > Advanced settings.
  • Go to Inbound Rules > New Rule.
  • Select Port > Next.
  • Choose TCP, enter 5432 as the specific local port > Next.
  • Select Allow the connection > Next.
  • Check all network types (Domain, Private, Public) > Next.
  • Name the rule something like "PostgreSQL Port 5432" > Finish.
5. Double-check your Django database configuration

Make sure your settings.py has the correct PostgreSQL credentials:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'your_database_name',  # You need to create this first in PgAdmin!
        'USER': 'postgres',  # Default PostgreSQL admin user
        'PASSWORD': 'your_postgres_password',  # The password you set during installation
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

Important: Django won’t create the database automatically—you need to make it in PgAdmin first (or use the createdb command in CMD).

6. Test the connection directly with psql

If you’re still stuck, test the connection using PostgreSQL’s built-in command-line tool to narrow down the issue:

  • Open CMD, run:
    psql -U postgres -h localhost -p 5432
    
  • If you can log in successfully, the problem is likely with your Django or PgAdmin settings. If it still errors out, go back through steps 1-4 to double-check.

内容的提问来源于stack exchange,提问作者Rahul Sharma

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 09:10:43