简体   繁体   中英

Django 1.11 can't connect to Postgres on RDS

I created a new EC2 Instance with Postgres on RDS. I confirmed that I can connect from the EC2 instance to the database using psql without any issue which means my security settings are fine.

However, when I try to run manage.py runserver or manage.py dbshell (from the virtualenv) Django hangs then eventually gives a timeout error:

psql: could not connect to server: Connection timed out Is the server running on host "whatever.rds.amazonaws.com" (172.xxx.xxx.xxx) and accepting TCP/IP connections on port 5342?

Traceback (most recent call last): File "manage.py", line 22, in execute_from_command_line(sys.argv) File "/home/ubuntu/Env/xxxx/lib/python3.5/site-packages/django/core/management/ init .py", line 363, in execute_from_command_line utility.execute()

File "/home/ubuntu/Env/ss2017/lib/python3.5/site-packages/django/core/management/ init .py", line 355, in execute self.fetch_command(subcommand).run_from_argv(self.argv)

File "/home/ubuntu/Env/ss2017/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv self.execute(*args, **cmd_options)

File "/home/ubuntu/Env/ss2017/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute output = self.handle(*args, **options)

File "/home/ubuntu/Env/ss2017/lib/python3.5/site-packages/django/core/management/commands/dbshell.py", line 22, in handle connection.client.runshell()

File "/home/ubuntu/Env/ss2017/lib/python3.5/site-packages/django/db/backends/postgresql/client.py", line 66, in runshell DatabaseClient.runshell_db(self.connection.get_connection_params())

File "/home/ubuntu/Env/ss2017/lib/python3.5/site-packages/django/db/backends/postgresql/client.py", line 58, in runshell_db subprocess.check_call(args)

File "/usr/lib/python3.5/subprocess.py", line 581, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command '['psql', '-U', 'db_name', '-h', 'whatever.rds.amazonaws.com', '-p', '5342', 'user_name']' returned non-zero exit status 2

I tried creating a new copy of the Django app to see if there were perhaps corrupt files involved, and I played with some changes to my settings.py file, but no luck.

Any ideas?

Edit :

Settings.py (the important bits)

DEBUG = False

ALLOWED_HOSTS = ['localhost', '0.0.0.0', '127.0.0.1', 'compute.amazonaws.com']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'database_name',
        'USER': 'xxxxxxxx',
        'PASSWORD': 'xxxxxxxx',
        'HOST': 'whatever.us-west-2.rds.amazonaws.com',
        'PORT': '5342',
    }
}

I ended up creating a local postgres database on the EC2 server to see if any errors come up. When I tried to run makemigrations or migrate to set up the database I got a "relation does not exist" error. The stack trace pointed to a line in one of my views that looked something like this:

some_queryset = Model.objects.all()
some_queryset.delete() # <--- this line was the problem

Even though this is technically an acceptable way to delete all entries in a given table, Django (on the server) did not like the fact that the relationships of the given model did not exist.

I commented out the line and was able to connect to the database without any issues.

So for future readers - if your Django app hangs on runserver , but you can reach the RDS database from the EC2 instance, try setting up a local database to check for any issues.

exit status 2 usually means you've called psql with the wrong arguments. For me, a psql authentication error also triggered this exit code. My problem ended up being a typo in the DBPASSWORD environment variable that settings.DATABASES['default'] was using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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