简体   繁体   English

远程连接到 Django postgres 数据库

[英]Connect to a Django postgres database remotely

I am using Postgres for a production system, and I would like to be able to connect to this database remotely to integrate the information with other systems.我将 Postgres 用于生产系统,我希望能够远程连接到该数据库以将信息与其他系统集成。

My configuration looks classic like follows :我的配置看起来很经典,如下所示:

ALLOWED_HOSTS = ['myipgoeshere', 'mydomainnamegoeshere']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'dbname',
        'USER': 'dbuser',
        'PASSWORD': 'dbpassword',
        'HOST': 'localhost'
    }
}

Now, if I want to connect to this database from another machine what should I use as hostname on that machine ?现在,如果我想从另一台机器连接到这个数据库,我应该在这台机器上使用什么作为主机名?

I need to provide information about我需要提供有关信息

Host
Database
Port
User
Password

In my config, host is localhost, but I cannot use this as remote hostname, should we use the IP address instead ?在我的配置中,主机是本地主机,但我不能使用它作为远程主机名,我们应该使用 IP 地址吗? Also will the port be the default postgres one?该端口也将是默认的 postgres 端口吗?

You can make the "HOST" like the usual host in a URL, for example a domain name or IP address.您可以将"HOST" URL 中的常用主机,例如域名或 IP 地址。

To specify the port, you can add a field called "PORT" to your database config.要指定端口,您可以在数据库配置中添加一个名为"PORT"的字段。 The default port for Postgres is 5432 , but can be any valid port. Postgres 的默认端口是5432 ,但可以是任何有效端口。

For example:例如:

DATABASES = {
    'default': {
        # ...
        'HOST': 'mydatabasehost.com', # e.g. or 192.168.0.10
        'PORT': '5432',
    }
}

我通过在默认情况下具有 localhost 的 postgress 配置文件中添加 IP 来解决问题,并在我开始在 django 配置中使用 IP 之后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM