连接到Django后端的AWS Postgres实例

时间:2022-06-25 02:46:11

I've created a PostgreSQL database on AWS, which I want to use for a Django project.

我在AWS上创建了一个PostgreSQL数据库,我想将它用于Django项目。

I've modified settings.py so that it has

我修改了settings.py以便它有

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': <db name>,
        'USER': <user>,
        'PASSWORD': <password>,
        'HOST': 'projectdevelopment.foobar.us-east-1.rds.amazonaws.com',
        'PORT': '5432',
    }
}

Which I would have thought seemed pretty straightforward. Except that when I try to make a migration I get this error:

我认为这看起来很简单。除了当我尝试进行迁移时,我收到此错误:

django.db.utils.OperationalError: could not translate host name "projectdevelopment.foobar.us-east-1.rds.amazonaws.com" to address: nodename nor servname provided, or not known

The value projectdevelopment.foobar.us-east-1.rds.amazonaws.com has been copied directly from the value under endpoint in the RDS console.

值projectdevelopment.foobar.us-east-1.rds.amazonaws.com已直接从RDS控制台中端点下的值复制。

Am I missing a setting, or misconfigured? Or both?

我错过了一个设置,还是配置错误了?或两者?

3 个解决方案

#1


6  

Make sure the server is up and running from Amazon's end.

确保服务器已启动并从Amazon端运行。

check this issue, They had same problem.

检查这个问题,他们有同样的问题。


OR

要么

Try to use 'ENGINE' = 'django.db.backends.postgresql_psycopg2', check this SO answer

尝试使用'ENGINE'='django.db.backends.postgresql_psycopg2',检查这个SO答案

Read: How To Use PostgreSQL with your Django Application on Ubuntu 14.04

阅读:如何在Ubuntu 14.04上使用PostgreSQL和Django应用程序


OR

要么

Try to assign DATABASE_URL for connecting RDS postgres to your django app.

尝试分配DATABASE_URL以将RDS postgres连接到您的django应用程序。

DATABASE_URL=postgis://<user>:<password>@<host>:<port>/<db-name>

Note: follow the same syntax for declaring DATABASE_URL, check these special characters in above url.

注意:按照相同的语法声明DATABASE_URL,请检查上面的url中的这些特殊字符。

  • : between user and password,
  • :用户和密码之间,
  • @ between password and host,
  • @密码和主机之间,
  • : between host and port,
  • :主机和端口之间,
  • / between port and db name
  • /端口和数据库名称之间

and assign it to DATABASES in settings.py

并将其分配给settings.py中的DATABASES

DATABASES['default'] = DATABASE_URL

In your case DATABASE_URL will be.

在您的情况下,DATABASE_URL将是。

postgis://<user>:<password>@projectdevelopment.foobar.us-east-1.rds.amazonaws.com:5432/<db name>

postgis:// @ projectdevelopment.foobar.us-east-1.rds.amazonaws.com:5432 /

  • 5432 is a default port used by postgres server
  • 5432是postgres服务器使用的默认端口

#2


2  

It looks like you are using the wrong engine. In addition to this, If you're not using a managed service like Elastic Beanstalk for your application, please make sure you add the security group of your RDS instance to the application environment, so that the application is able to access the RDS instance.

看起来你使用的是错误的引擎。除此之外,如果您没有为应用程序使用Elastic Beanstalk等托管服务,请确保将RDS实例的安全组添加到应用程序环境中,以便应用程序能够访问RDS实例。

If you're using Elastic Beanstalk: Click here or here

如果您使用的是Elastic Beanstalk:点击此处或此处

If you're not using elastic beanstalk, most likely the issue is with VPC. Please refer this discussion, to configure EC2 directly with RDS. The scenario is explained well in the official docs here.

如果你没有使用弹性beanstalk,很可能是VPC的问题。请参考此讨论,直接使用RDS配置EC2。这里的官方文档很好地解释了这个场景。

#3


2  

Your host string is wrong.

您的主机字符串错误。

(venv) duddits@dev:~/movina$ host projectdevelopment.foobar.us-east-1.rds.amazonaws.com
Host projectdevelopment.foobar.us-east-1.rds.amazonaws.com not found: 3(NXDOMAIN)


(venv) duddits@dev:~/movina$ dig  projectdevelopment.foobar.us-east-1.rds.amazonaws.com

; <<>> DiG 9.10.3-P4-Ubuntu <<>> projectdevelopment.foobar.us-east-1.rds.amazonaws.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 30254
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;projectdevelopment.foobar.us-east-1.rds.amazonaws.com. IN A

;; AUTHORITY SECTION:
us-east-1.rds.amazonaws.com. 55 IN      SOA     ns-1420.awsdns-49.org. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400

;; Query time: 0 msec
;; SERVER: 109.197.120.67#53(109.197.120.67)
;; WHEN: Thu Jan 25 04:43:44 +07 2018
;; MSG SIZE  rcvd: 164

This means that there is no host with name projectdevelopment.foobar.us-east-1.rds.amazonaws.com and you'll not be able to connect to it as there is no way to distinguish it's ip address. So you should find correct hostname.

这意味着没有名称为projectdevelopment.foobar.us-east-1.rds.amazonaws.com的主机,您将无法连接到它,因为无法区分它的IP地址。所以你应该找到正确的主机名。

#1


6  

Make sure the server is up and running from Amazon's end.

确保服务器已启动并从Amazon端运行。

check this issue, They had same problem.

检查这个问题,他们有同样的问题。


OR

要么

Try to use 'ENGINE' = 'django.db.backends.postgresql_psycopg2', check this SO answer

尝试使用'ENGINE'='django.db.backends.postgresql_psycopg2',检查这个SO答案

Read: How To Use PostgreSQL with your Django Application on Ubuntu 14.04

阅读:如何在Ubuntu 14.04上使用PostgreSQL和Django应用程序


OR

要么

Try to assign DATABASE_URL for connecting RDS postgres to your django app.

尝试分配DATABASE_URL以将RDS postgres连接到您的django应用程序。

DATABASE_URL=postgis://<user>:<password>@<host>:<port>/<db-name>

Note: follow the same syntax for declaring DATABASE_URL, check these special characters in above url.

注意:按照相同的语法声明DATABASE_URL,请检查上面的url中的这些特殊字符。

  • : between user and password,
  • :用户和密码之间,
  • @ between password and host,
  • @密码和主机之间,
  • : between host and port,
  • :主机和端口之间,
  • / between port and db name
  • /端口和数据库名称之间

and assign it to DATABASES in settings.py

并将其分配给settings.py中的DATABASES

DATABASES['default'] = DATABASE_URL

In your case DATABASE_URL will be.

在您的情况下,DATABASE_URL将是。

postgis://<user>:<password>@projectdevelopment.foobar.us-east-1.rds.amazonaws.com:5432/<db name>

postgis:// @ projectdevelopment.foobar.us-east-1.rds.amazonaws.com:5432 /

  • 5432 is a default port used by postgres server
  • 5432是postgres服务器使用的默认端口

#2


2  

It looks like you are using the wrong engine. In addition to this, If you're not using a managed service like Elastic Beanstalk for your application, please make sure you add the security group of your RDS instance to the application environment, so that the application is able to access the RDS instance.

看起来你使用的是错误的引擎。除此之外,如果您没有为应用程序使用Elastic Beanstalk等托管服务,请确保将RDS实例的安全组添加到应用程序环境中,以便应用程序能够访问RDS实例。

If you're using Elastic Beanstalk: Click here or here

如果您使用的是Elastic Beanstalk:点击此处或此处

If you're not using elastic beanstalk, most likely the issue is with VPC. Please refer this discussion, to configure EC2 directly with RDS. The scenario is explained well in the official docs here.

如果你没有使用弹性beanstalk,很可能是VPC的问题。请参考此讨论,直接使用RDS配置EC2。这里的官方文档很好地解释了这个场景。

#3


2  

Your host string is wrong.

您的主机字符串错误。

(venv) duddits@dev:~/movina$ host projectdevelopment.foobar.us-east-1.rds.amazonaws.com
Host projectdevelopment.foobar.us-east-1.rds.amazonaws.com not found: 3(NXDOMAIN)


(venv) duddits@dev:~/movina$ dig  projectdevelopment.foobar.us-east-1.rds.amazonaws.com

; <<>> DiG 9.10.3-P4-Ubuntu <<>> projectdevelopment.foobar.us-east-1.rds.amazonaws.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 30254
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;projectdevelopment.foobar.us-east-1.rds.amazonaws.com. IN A

;; AUTHORITY SECTION:
us-east-1.rds.amazonaws.com. 55 IN      SOA     ns-1420.awsdns-49.org. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400

;; Query time: 0 msec
;; SERVER: 109.197.120.67#53(109.197.120.67)
;; WHEN: Thu Jan 25 04:43:44 +07 2018
;; MSG SIZE  rcvd: 164

This means that there is no host with name projectdevelopment.foobar.us-east-1.rds.amazonaws.com and you'll not be able to connect to it as there is no way to distinguish it's ip address. So you should find correct hostname.

这意味着没有名称为projectdevelopment.foobar.us-east-1.rds.amazonaws.com的主机,您将无法连接到它,因为无法区分它的IP地址。所以你应该找到正确的主机名。