Django - Postgres connection - django

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',
}
}

Related

How to share database details in Github correctly, using Django and PostgreSQL?

I have an assignment in which I need to set up a database server using Python Django and PostgreSQL. I need to assign the project in Github, and the grader will use my repository to check my project.
In my setting.py file I have the following lines:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'CourseDB',
'USER': 'postgres',
'PASSWORD': '123',
'HOST': 'localhost',
'PORT': '5432'
}
}
What to do so the details on my file will be correct for the grader's side?
Will they have to create a database with the given name, user and password like the ones in my file?
I think that maybe for the database name, I can add in the readme to run CREATE DATABASE CourseDB first. But then again, I don't know their user and password on their machine, So I don't know what should be written in my file in order for my code to work on their machine.
I followed this tutorial on YouTube to create my file.
Unless you need some Postgres specific functionality, you can use the default SQLite backend:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
Django will automatically create a database file. No setup/user/password needed.
In general, a good practice is to save all the DB sensitive credentials in settings.py using env vars like the following:
import os
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ['DATABASE_NAME'],
'USER': os.environ['DATABASE_USER'],
'PASSWORD': os.environ['DATABASE_PASSWORD'],
'HOST': os.environ['DATABASE_HOST'],
'PORT': os.environ['DATABASE_PORT'],
}
}
Several DB backends are natively supported (see the official docs here) including PostgreSQL, MariaDB, MySQL, Oracle and SQLite and a number of additional backends provided by third parties.
If you go with the SQLite backend (for sure the best alternative for quick development and MVPs), keep in mind that there are some differences specific to the SQLite backend that you should take into consideration (e.g. not supported features).

Connecting a Neo4j graph database to a Django REST API app

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

Connecting to a remote db through a jump server in Django

I'm trying to tunnel my db connection in a django application through a jump server but can't seem to get it working because django manage.py handles & process the connections.
here's that I have in the settings.py
#process ssh_key first
ssh_key= os.getenv('SSH_KEY', '').encode('utf8').decode('unicode_escape')
server ={}
with sshtunnel.open_tunnel(
(os.environ.get('SSH_HOST'),int(os.getenv('SSH_PORT'))),
ssh_pkey=paramiko.RSAKey.from_private_key(io.StringIO(ssh_key)),
ssh_username= os.environ.get('SSH_USERNAME'),
remote_bind_address=(os.environ.get('DB_HOST'), int(os.getenv('DB_PORT'))),
) as ssh_proxy_host:
server={
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'HOST': 'localhost',
'PORT': ssh_proxy_host.local_bind_port,
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASS'),
}
# here's where I should have the connection function to db, but don't know if django has that option available
The true remote host is specified upon tunnel creation. The HOST used for the db connection should be localhost because you need it to find your end of the tunnel, not the other end of it.

Connect django web app to postgresql in gcloud

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': '',
}
}
'''

How to two apps from django to conect one db postgresql

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.)