You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

执行python manage.py makemigrations时PostgreSQL连接拒绝错误求助

解决Django执行makemigrations时PostgreSQL连接拒绝的问题

先还原下你遇到的错误场景:当执行python manage.py makemigrations命令时,抛出了连接拒绝的错误:

(myproject) D:\E\Project\Trials\Book inventory Assignment>python manage.py makemigrations
Traceback (most recent call last):
File "C:\Users\Krishna Veer Singh\Envs\myproject\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection
self.connect()
File "C:\Users\Krishna Veer Singh\Envs\myproject\lib\site-packages\django\db\backends\base\base.py", line 195, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\Krishna Veer Singh\Envs\myproject\lib\site-packages\django\db\backends\postgresql\base.py", line 178, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\Krishna Veer Singh\Envs\myproject\lib\site-packages\psycopg2\__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?

django.db.utils.OperationalError: could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?

你的settings.py数据库配置为:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'inventory_database',
        'USER': 'manikaran',
        'PASSWORD': 'Aossie',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

这个错误本质是Django无法连接到PostgreSQL服务,咱们按优先级一步步排查解决:

1. 先确认PostgreSQL服务是否正在运行

这是最常见的触发原因,Windows系统下可以这么操作:

  • 按下Win + R,输入services.msc回车打开服务管理器
  • 在列表里找到类似postgresql-x64-XX(XX是你的PostgreSQL版本号,比如15)的服务
  • 如果状态显示“已停止”,右键点击选择“启动”;建议把启动类型设为“自动”,避免下次重启系统后服务又停了
  • 也可以用命令行启动:打开管理员命令提示符,输入net start postgresql-x64-XX(替换成你的实际版本号)

2. 检查PostgreSQL的端口和监听配置是否匹配

确认你的PostgreSQL确实在监听127.0.0.1的5432端口:

  • 找到PostgreSQL安装目录下的data文件夹(比如C:\Program Files\PostgreSQL\15\data
  • 打开postgresql.conf文件,找到listen_addresses配置项,确保它的值包含127.0.0.1(比如listen_addresses = 'localhost,127.0.0.1'
  • 同时检查port配置项,确认值为5432,和你settings里的配置一致
  • 打开pg_hba.conf文件,确保存在允许本地TCP连接的规则,比如:
    host    all             all             127.0.0.1/32            scram-sha-256
    

修改完配置后,记得重启PostgreSQL服务让配置生效

3. 确认目标数据库是否存在

你的settings里指定的数据库是inventory_database,需要确认这个数据库已经创建:

  • 打开pgAdmin(如果已安装),连接到本地PostgreSQL服务器,查看是否存在该数据库
  • 或者用命令行操作:打开psql(PostgreSQL自带的命令行工具),输入CREATE DATABASE inventory_database;创建数据库
  • 同时确认用户manikaran有访问该数据库的权限,可在psql里执行GRANT ALL PRIVILEGES ON DATABASE inventory_database TO manikaran;

4. 检查防火墙是否拦截了连接

Windows防火墙可能会阻止Django访问PostgreSQL的5432端口:

  • 打开Windows Defender防火墙,点击“高级设置”
  • 在“入站规则”里,找到PostgreSQL相关的规则,确保状态为“允许”;如果没有相关规则,新建一个允许5432端口TCP连接的入站规则

排查完这些步骤后,重新运行python manage.py makemigrations,应该就能正常连接数据库了。

内容的提问来源于stack exchange,提问作者krishna veer

火山引擎 最新活动