【问题标题】:Pycharm Pro connection to PostgreSQL database via SSH tunnel doesn't work with DjangoPycharm Pro 通过 SSH 隧道连接到 PostgreSQL 数据库不适用于 Django
【发布时间】:2021-09-01 07:32:24
【问题描述】:

我在 Pycharm Pro 中添加了一个 PostgreSQL 数据源,它使用 SSH 隧道访问数据库,位于可通过 VPN 访问的远程服务器上。我想在我的 Django 项目中访问这个数据库。数据库参数为the following(白条隐藏了远程服务器的公网IP)和SSH参数like this(我的用户名和服务器公网IP也被隐藏了)。

我在 settings.py 文件中使用相同的 credentials

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'rhu4bdd',
        'USER': 'rhu4gres',
        'PASSWORD': <password>,
        'HOST': <public IP>,
        'PORT': '5432'
    }
}

Django 返回此错误:

django.db.utils.OperationalError: could not connect to server: Connection timed out
    Is the server running on host "<public IP>" and accepting
    TCP/IP connections on port 5432?

如何在我的 Django 项目中使用我的 Pycharm 数据库连接?

谢谢,

MB

【问题讨论】:

    标签: python django postgresql pycharm


    【解决方案1】:

    使用关注

    ssh -L 5432:127.0.0.1:5432  root@<public IP>
    

    之后在您的 Django 配置中使用 127.0.0.1 作为数据库主机。

    【讨论】:

    • 谢谢@Dips,我用这个,但我得到“密码错误”,但我确定这是一个好密码...django.db.utils.OperationalError: FATAL: password authentication failed for user "rhu4gres" FATAL: password authentication failed for user "rhu4gres"
    • 这肯定会打开与数据库服务器的连接,但对于密码问题,您需要检查 pg_hba 和日志文件。
    猜你喜欢
    • 2021-12-02
    • 2016-04-30
    • 2014-07-20
    • 2015-03-01
    • 1970-01-01
    • 2013-04-05
    • 2014-09-17
    • 1970-01-01
    • 2021-02-01
    相关资源
    最近更新 更多