Unable to connect aws RDS to django local - django

I am trying to connect my local django application to amazon RDS (tried with both MySQL and PostgreSQL) but I am not able to connect as it shows the following errors, Currently seeking answer for PostgreSQL.
In my Settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'database_name',
'USERNAME': 'my_username',
'PASSWORD': 'my_password',
'HOST': 'database-abc.xxx.us-region-yyy.rds.amazonaws.com',
'PORT': '5432',
}
}
In AWS database configuration:
I get this error:
django.db.utils.OperationalError: could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "database-abc.xxx.us-region-yyy.rds.amazonaws.com" (69.420.00.121) and accepting TCP/IP connections on port 5432?
I referred to all the available data but still am unable to resolve this issue!
Thanks in advance!

In the VPC security group go to inbound settings create a new rule with the Postgres connection type, not HTTP and make it available from every.
Note: don't forget to make accessibility to the database to be public.

Related

What do I do to fix my PostgreSQL database connection problem in Django?

I am trying to connect my Django project to a PostgreSQL database I created on AWS but I keep getting this error message each time I run py manage.py runserver.
Here is the error message:
django.db.utils.OperationalError: connection to server at "database-1.ce7oz69tjzjc.us-east-1.rds.amazonaws.com" (52.3.150.111), port 5432 failed: Connection timed out (0x0000274C/10060)
Is the server running on that host and accepting TCP/IP connections?
I followed the tutorial on w3school and followed all instructions. I created a PostgreSQL database then I updated my settings with the following:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'codekeen',
'USER': 'baseuser',
'PASSWORD': 'database',
'HOST': 'database-1.ce7oz69tjzjc.us-east-1.rds.amazonaws.com',
'PORT': '5432'
}
}
as I was told to, but I keep getting the error each time try migrating or running the server.
What do I do to fix this?
First of all, make sure that postgresql is up and running. You can check with systemctl status postgresql-xx where xx is your postgresql version. After that,You need to allow TCP/IP connections to be able to connect postgresql database. Open the postgresql.conf file
vim /etc/postgresql/8.2/main/postgresql.conf
or
vim /var/lib/pgsql/data/postgresql.conf
And set the listen addresses to "*" or IP addresses.
listen_addresses='*'
or
listen_addresses='xxx.xx.x.x'

Django can not connect to PostgreSQL DB

When I`m trying to connect Django Server to PostgreSQL db there is an error:
" port 5433 failed: Connection refused Is the server running on that host and accepting TCP/IP connections? "
I`m using Windows 10, Pycharm, Debian
Settings in Django:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'ps_store_db',
'USER': 'zesshi',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5433',
}
}
Tried to check connection with DBeaver and all`s good there, but still cant connect with Django
My firewall is off, i was trying to change from 5432 to 5433
Dbeaver connection
Dbeaver connection 2
Try restarting/reinstalling postgres. Most likely DBeaver has blocked the port that's why you are not able to connect from django.
(Sorry for posting answer, i am unable to comment yet)
The default port of the PG database is 5432. If you need to change this port, you need to edit the postgresql.conf file and restart the database service before the client can access it.
You also need to check the pg_hba.conf file. The recommended configuration is as follows:
host all all 0.0.0.0/0 md5

Google cloud run cannot reach Cloud SQL

Everything was working fine just a moment ago but suddenly Google Cloud Run cannot connect with Cloud SQL.
Both Cloud Run and Cloud SQL are in same project. Cloud SQL has public IP.
Cloud Run is running a containerized Django/uwsgi/nginx application. Getting following error:
MySQLdb._exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'xx.xxx.xx.xxx:3306' (110)")
Django settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': my_db_name,
'USER': my_db_user,
'PASSWORD': my_db_password,
'HOST': cloud_sql_ip_address,
'PORT': '3306',
}
}
Below is the Cloud Run yaml piece:
annotations:
run.googleapis.com/client-name: gcloud
client.knative.dev/user-image: my_custom_manage
run.googleapis.com/client-version: 347.0.0
run.googleapis.com/cloudsql-instances: my_project_id:us-central1:my_sql_server
autoscaling.knative.dev/maxScale: '10'
run.googleapis.com/sandbox: gvisor
I have also checked this - https://cloud.google.com/sql/docs/mysql/connect-run
Service account that is attached to Cloud Run service have Compute Engine default service account which basically means it has all the access.
Solution:
For anyone who may come to this question, use socket reference, not IP and Port. Since Cloud Run creates socket to connect to Cloud SQL and Django's IP:3306 doesn't work.
My update django DB settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': my_db_name,
'USER': my_db_user,
'PASSWORD': my_db_password,
'HOST': f'/cloudsql/cloud_sql_connection_name'
}
}
I am not sure though why it was working fine before with IP:3306, Cloud Run should have given error in the start itself.
Cloud Run uses an unix socket to connect to SQL.
From your error message, it looks like it tries to connect directly to the IP.
I would check the application code and see if there was an undetected update, the connection string should be based on a socket and not on an IP,
socket format is: /cloudsql/connection_id
See more here

Error with Django application on Cloud Run using Cloud SQL PostgreSQL DB - cannot connect to database

I have this Django project on Google Cloud on Cloud Run. It is supposed to connect to the Cloud SQL PostgreSQL database and work, but it doesn't. Here is the database part of settings.py (I have removed the private data):
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db',
'USER': 'user',
'PASSWORD': 'password',
'HOST': '/cloudsql/project:europe-west4:instance',
'PORT': '5432',
}
}
However, when I try to do anything database-related (for example, log in), it throws the error
OperationalError at /accounts/login/
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/cloudsql/*project*:europe-west4:*instance*/.s.PGSQL.5432"?
If I remove the /cloudsql/ from HOST it gives the error
OperationalError at /accounts/login/
could not translate host name "*project*:europe-west4:*instance*" to address: Name or service not known
When I use the Cloud SQL proxy exe to test on my local computer it works. However, when I deploy it to Cloud Run it doesn't.
Have you enabled connecting to that Cloud SQL instance from Cloud Run?
Try running
gcloud run services update [your cloud run service name] \
--add-cloudsql-instances [project:region:instance]
Also check that your Cloud Run service account has Cloud SQL Client permissions. See this documentation for more information on how to do that.
Try using the full url in host parameter

Docker messed with local django/postgres projects. Can't run project on localhost

I stocked at postgresql/django issue. After attempt of running the localhost server got the following error
django.db.utils.OperationalError: FATAL: no pg_hba.conf entry for host "172.17.0.1", database "closerdb", SSL off
According to many similar issues and answers last lines at pg_hba.conf look this way:
host all all 0.0.0.0/0 md5
local replication postgres trust
host replication postgres 127.0.0.1/32 trust
host replication postgres 172.17.0.1 trust
host replication postgres ::1/128 trust
I have restarted postgres after editing .conf file, but error remains. I supposed that it was cause by docker installation, because normally django project has to run on loclahost(127.0.0.1) and 172.17.0.1 is a default docker ip.
How to fix all that and avoid such problems in future?
EDIT: django database settings. worked fine previously before installing docker:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'closerdb',
# 'HOST': '/tmp/mysql.sock',
# 'PORT': '8000',
# 'HOSTNAME': '',
'USER': 'thekotik',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '',
}
}
I assume your django just wants to connect to a database called closerdb. In this case the corresponding pg_hba.conf entry should look like that:
host closerdb postgres 172.17.0.1 trust
From the postgresql docs:
The value replication specifies that the record matches if a replication connection is requested (note that replication connections do not specify any particular database). Otherwise, this is the name of a specific PostgreSQL database
I also want to point out that you should use trust carefully depending on your environment, because:
Allow the connection unconditionally. This method allows anyone that can connect to the PostgreSQL database server to login as any PostgreSQL user they wish, without the need for a password or any other authentication.