Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to AWS Postgres Instance For Django Backend

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

I've modified settings.py so that it has

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.

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

like image 740
Batman Avatar asked Jan 18 '18 03:01

Batman


3 Answers

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

check this issue, They had same problem.


OR

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

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


OR

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

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.

  • : 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

DATABASES['default'] = DATABASE_URL

In your case DATABASE_URL will be.

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

  • 5432 is a default port used by postgres server
like image 68
Satendra Avatar answered Sep 30 '22 11:09

Satendra


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.

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

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.

like image 20
Abhishek Menon Avatar answered Sep 30 '22 11:09

Abhishek Menon


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.

like image 37
duddits Avatar answered Sep 30 '22 13:09

duddits