在Django数据库设置中使用en0地址作为主机

问题描述 投票:0回答:1

我试图在docker Django容器中使用我的localhost数据库。我已经在postgresql.conf文件中允许了listen_address。我在pg_hba.conf文件中添加了host all all localhost,192.168.1.9 trust

192.168.1.9是我的en0地址。现在我想在django的数据库设置中使用192.168.1.9作为我的主机。

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'db_name',
        'USER': 'db_user',
        'PASSWORD': 'db_password',
        'HOST': '192.168.1.9',
        'PORT': '5432',
    }
}

我正在尝试这个,但我无法成功。难道我做错了什么?我希望postgres接受所有连接,因此Django app容器可以连接到我的本地机器数据库。我在尝试

psql -h 192.168.1.9 -U db_user -d db_name

Getting psql: could not connect to server: Permission denied
    Is the server running on host "192.168.1.9" and accepting
    TCP/IP connections on port 5432?

我不确定我在做什么错。

django docker-compose psql
1个回答
0
投票

docker容器通常位于单独的网络接口(docker0)上,因此如果您的应用程序想要访问您的主机,则必须使用docker0接口。

你可以从容器中获取主机的ip

/sbin/ip route|awk '/default/ { print $3 }'
© www.soinside.com 2019 - 2024. All rights reserved.