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>'
}
}
Related
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.
I am trying to connect a remote Neo4j database to a Django app with a REST API.
I am trying to specify the database in the settings.py file using the following code:
DATABASES = {
'default': {
'NAME': 'papers.db',
'ENGINE': 'django.db.backends.sqlite3',
'USER': '',
'PASSWORD': '',
'PORT': '',
},
}
I would like to know:
What python libraries need to be installed in order to do this?
What is the 'ENGINE' that needs to be specified in the code above?
The Neo4j database has a URI, but does not provide us with an IP address - am I able to use this URI?
I am confident that I know what the other parameters need to be.
Thanks in advance
Currently I have a requirement,I need to communicate with external server using django application. The server is already up,Next section is the data transfer. I need some time samples and values from server and I need to send responses back to server. How django port can listen to external server.? How it can response back? I need asynchronous communication and REST responses
According to the comments, the external server is a PostgreSQL database.
Since you're using Django, you can easily set this up as a secondary database in your settings:
DATABASES = {
'default': # that SQLite config...,
'game': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': 'somewhere-else',
'PORT': '5432',
},
}
You don't have to route any models to that database, but if you do create any models to correspond to the data in the game server, you'll want to set managed = False so Django doesn't do migrations or anything.
If you don't want to use models, just open a cursor to the secondary database and query away:
from django.db import connections
with connections['game'].cursor() as cursor:
cursor.execute('SELECT something FROM some_table')
# etc...
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': '',
}
}
'''
I have a little question.
I have an app in Django rest framework with PostgreSQL(the project called djangoFall), and I build other projects with Django called djangoRuim, but I don't know how to connect and read the tables in the djangoRuim for example
djangoFall connect with the PostgreSQL is working
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'colonybitdb0',
'USER': 'postgres',
'PASSWORD': 'root2017',
'HOST': '127.0.0.1',
'PORT': '5432',
} }
in here I can read tables like this
from djangoFall.profile_clbt.models import HelperNotificationMsg
djangoRuim connect with the same connect PostgreSQL DB is working but I don't how to read the tables
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'colonybitdb0',
'USER': 'postgres',
'PASSWORD': 'root2017',
'HOST': '127.0.0.1',
'PORT': '5432',
}}
in here I can't read the same table
from .models import HelperNotificationMsg # wrong true ?
because here I don't have models.
please help me, how to read these tables.
You can use the connection object to execute direct SQL query to the table's name generated by Django in the second app or the value you assigned to db_table in the Meta option in the second app models.py. This will work here because you are using the same database settings for the two apps.
In case of different database settings, you will need to explicitly connect to the external database with the driver and execute the SQL query.
That being said, since you are using the same database, I think you should consider merging those two apps, or package one. But it depends on what you're trying to archive and the overall architecture.
If it is really mandatory to access to the very same database from two different django applications, here is what needs to be done:
Create the same object model in both applications, make sure each object member are identical.
Use the same credentials and url to access the database.
Now, both of these applications can access to the database with implicit race condition protection, presuming that the database has native support for that. (All modern databases have that support.)