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.
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, typeservices.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).
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.
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.confwith 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).
- Find the line
- Edit
pg_hba.conf(also in the data folder):- Add this line at the end to allow local TCP/IP connections with secure authentication:
Note: For older PostgreSQL versions, you might usehost all all 127.0.0.1/32 scram-sha-256md5instead ofscram-sha-256.
- Add this line at the end to allow local TCP/IP connections with secure authentication:
- Restart the PostgreSQL service (go back to
services.msc, right-click > Restart).
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
5432as 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.
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).
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

