Connect django web app to postgresql in gcloud - django

i have been struggling to connect my django web app to a PostgreSQL instance which I set up inside my gcloud account for testing purposes.
I have done the following DB configs in settings.py in Django:
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'instance connection name from gcloud',
'USER' : 'postgres',
'PASSWORD': 'passsss',
'HOST': 'ip-address',
'PORT': '5432',
}
}
the error that I receive in Django after trying to migrate is :
django.db.utils.OperationalError: FATAL: database "..instance connection name from gcloud.." does not exist
I have tried creating a new database "django" and adding it to the NAME with:. This did not work as well. I have also configured in gcloud connections my own computer IP as an authorized network
Normally if I use service like elephantsql it works fine.
Any help would be appreciated. Thanks!

From what I understood from your post, is that you wish to connect from your local pc to a Cloud SQL PostgreSQL instance.
I see that you seem to be following the official documentation already, as you mentioned that you added your IP to authorized networks in order to be able to connect externally.
The cause of your error seems to be due to the format of the NAME parameter.
Are you passing it in this format?
PROJECT:ZONE:INSTANCENAME
Here you have all the available options on how to connect externally to a Cloud PostgreSQL instance.
And here you have the documentation of the requirements to connect an external application to a Cloud SQL PostgreSQL instance.

ok finally after a lot of research and wasting couple of hours I managed to find Django configs in digital ocean docu... pretty weird
This works:
'''
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'my_django_db',
'USER' : 'user1',
'PASSWORD': 'pass',
'HOST': 'ipv4',
'PORT': '',
}
}
'''

Related

Django - Postgres connection

I am super beginner, but want to learn super fast building web application.
I am right now developing an Income-Expense web app on Django Framework (Python, js and Ajax).
I am now stuck with the server and get different errors. Anyone can support me ?
ERROR
"django.db.utils.OperationalError: connection to server on socket "/tmp/.s.PGSQL.5432" failed: fe_sendauth: no password supplied"
I think I shut down everything not properly and when a came back my virtual environment was not working.
Thank You
Don't know what to try more
To resolve the issue, make sure that you have the correct credentials to access your PostgreSQL database. You may need to update the database settings in your Django settings.py file to include the correct database name, username, password, host, and port information.
for example
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '5432',
}
}

Connect Django to SQL database in Google Cloud's Compute Engine

My Django app has been working successfully using Google's App Engine standard environment. However I need to use Compute Engine for more power. I am testing a single VM instance running the app, however it has issues connecting to the POSTGRES database.
The compute engine service account has all the same permissions as the app engine service account.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'HOST': '/cloudsql/myproject:us-central1:mypostgresname',
'NAME': 'mydatabasename',
'USER': 'myusername',
'PASSWORD': 'mypassword',
}
}
I had success changing the "HOST" field to the public IP address instead of that /cloudsql directory.

Django is trying to authenticate Wrong user for AWS RDS and failing

I am hosting a postgresql database on AWS using the RDS service. I am trying to connect the django project to the aws database using the settings.py file in the django project. I keep getting the following error:
connection to server at "database-1.xxxxxx.us-east-1.rds.amazonaws.com" (xx.xxx.xx.xxx), port 5432 failed: FATAL: password authentication failed for user "local_user"
This error is unexpected because it should not be trying to authenticate the local_user as this is the user for my local postgres server, it should be trying to authenticate the user for the hosted database, which is completely different.
This is what my settings file looks like:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ.get('DATABASE_NAME'),
'USER': os.environ.get('DATABASE_USER'),
'PASSWORD': os.environ.get('DATABASE_PASSWORD'),
'HOST': "database-1.xxxxxx.us-east-1.rds.amazonaws.com",
'PORT': 5432
}
}
I can't figure out what the issue seems to be. Any help will be appreciated!
I named the environment variable wrong so USER was none!

Why am I getting error "Client with IP address x.x.x.x isnt allowed to connect to this MySql server" when trying to connect to MySql from Django?

I am trying to create tables for my models in this full stack project (Django/React). After I run the python manage.py migrate command I'm expecting to see:
Migrations for X:
X\migrations\0001_initial_py
- Create model Departments
- Create model Employees
Instead I get the following error: (9000, "Client with IP address 'X' is not allowed to connect to this MySQL server."
I have tried the command pip install pymysql
then edited the settings.py file adding
import pymysql
pymysql.install_as_MySQLdb()
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mytestdb',
'USER': 'testadmin#mytestmysql',
'PASSWORD': 'XXXXXXXXXX#',
'HOST': 'mytestmysql.mysql.database.XXXXXXXXXXXXX.com',
'PORT': '3306'
MySQL permissions are such that you have to specify 'user'#'host' when defining privileges. My guess is that you didn't set the MySQL database user up correctly.
Read here to understand what I'm referring to.

Way to query CloudSql from Django app

I am trying to find an effective way to query a Google Cloud SQL database from my Django app. This app is not hosting on App Engine, just on a local server.
I have found a couple of links:
This one is a generic python connector
This answer mentions a way to do it from a local Django app, but it looks like it is more for testing.
I am not looking to use CloudSQL as my app backend, just make occasional queries to it (probably something daily to read all records in one CloudSQL table, and update a local database with the result)
This is possible, and not just for testing. You want something like:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'name_of_your_database',
'USER': 'root',
'PASSWORD': 'your_cloud_sql_root_password',
'HOST': 'assigned_IP_of_your_cloud_sql_instance',
'PORT': '3306',
}
}
Where name_of_your_database was the database you created with CREATE DATABASE.
You may also want to use SSL to protect your data as it goes over the public internet. To do this configure SSL for your instance and then add the following to the default django database options:
'OPTIONS': {
'ssl': {
'ca': '<PATH TO CA CERT>',
'cert': '<PATH TO CLIENT CERT>',
'key': '<PATH TO CLIENT KEY>'
}
}