Unable to migrate changes to schema to database using prisma - digital-ocean

When I tried running
npx prisma migrate dev --name init
It returns the following error
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "defaultdb", schema "public" at "sampleDB-do-user-12586073-0.b.db.ondigitalocean.com:25070"
Error: P1001: Can't reach database server at `sampleDB-do-user-12586073-0.b.db.ondigitalocean.com`:`25070`
Please make sure your database server is running at `sampleDB-do-user-12586073-0.b.db.ondigitalocean.com`:`25060`.
I hosted my postgres database on digital ocean and added the connection string to my .env file
This is what my schema.prisma file like
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id Int #default(autoincrement()) #id
username String #unique
password String?
}

Related

Migrate DB on Railway: ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value

I want to migrate my db to Railway Postgres.
I put my DATABASE_URL as a env variable in prod.env file:
DATABASE_URL='postgresql://postgres:(my password here)F#containers-us-west-97.railway.app:6902/railway'
Here how I import it in my prod settings file:
DATABASE_URL = os.getenv("DATABASE_URL")
DATABASES = {
"default": dj_database_url.config(default=DATABASE_URL, conn_max_age=1800),
}
When I try to migrate the db:
./manage.py migrate --settings=app.settings.prod
I get an error:
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check the settings documentation for more details.
I used the same approach when I migrated my DB to Heroku, and it worked well.
I checked that correct value comes DATABASE_URL to prod settings file when I debug it.
I also added DATABASE_URL as a variable to my Railway project.
UPD. I tried to hardcode my DATABASE_URL in the settings file, and it worked well. But again, even when I print my DATABASES after this code:
DATABASES = {
"default": dj_database_url.config(default=DATABASE_URL, conn_max_age=1800),
}
I see that the values are correct.
How can I resolve this?

Test and Verify AWS Redis Integration with Django project

I am new to Django. I was trying to implement Redis cache system into my Django project. I am using AWS free tier to host my Django project on EC2 machine using gunicorn web server and trying to integrate AWS Redis Cache. I have added below entry in my settings.py file:
CACHE = {
'default': {
'BACKEND' : "redis_cache.cache.RedisCache",
'LOCATION' : "redis://xxx.xxx.xxxxx.cache.amazonaws.com/1",
'OPTIONS' : {
'CLIENT_CLASS' : 'redis_cache.client.DefaultClient',
},
}
}
And below is my view function:
def usertable(request):
obj = userdetails.objects.get(id=1)
name = obj.name
if cache.get(name):
cache_name = cache.get(name)
print ("From CACHE")
else:
cache_name = obj.name
cache.set(name, cache_name)
print ("*****************FROM DB********************")
context = {
'name' : cache_name,
}
This code is working for me and I can see From CACHE printed in my terminal. But the key value pair which is set if I manually connect to redis using below cli tool:
redis-cli -h xx.xx.xxxxx…cache.amazonaws.com -p 6379 -n 1
on giving keys * I do not see any key value pair is set.
I am not sure if this is the correct way to test integration of Redis cache. Kindly advice if anyone had tried Redis Cache system.

django.db.utils.OperationalError: FATAL: database does not exist (postgres / deploy to digitalocean)

I am trying to deploy a project with digital ocean. I followed the instructions found at https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
Some of the important ones:
I ran:
postgres=# CREATE DATABASE jobzumoDB;
CREATE DATABASE
then:
postgres=# CREATE USER admin WITH PASSWORD '123';
CREATE ROLE
postgres=# GRANT ALL PRIVILEGES ON DATABASE jobzumoDB TO admin;
GRANT
set the following in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'jobzumoDB',
'USER':'admin',
'PASSWORD':'123',
'HOST':'localhost',
'PORT':'',
}
then tried to run:
~/jobzumo/manage.py makemigrations
and got:
File "/home/justin/jobzumo/env/lib/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: FATAL: database "jobzumoDB" does not exist
Two things:
ALLOWED_HOSTS = ['jobzumo.com', '142.93.184.125']
I have not yet connect jobzumo.com to digital ocean, but the IP address was copied from my droplet.
Also, I ran: pip install django gunicorn psycopg2 (from digitial ocean)
but a tutorial on youtube said it was very important to now
install psycopg2-binary instead, however, I did not do this as the video was veering far from digital ocean's tutorial.
Thanks for any help, after starting to understand django I didn't think deploying would be this much of a headscratcher.
Formalizing what we worked out in the comments as an answer, when you give postgres an unquoted string as an identifier, it forces it to lower-case. see this similar answer from pgsql-general mailing list. So the actual name of the database created by the command CREATE DATABASE jobzumoDB; is jobzumodb. to create a database named jobzumoDB it's necessary to use quotes, as in CREATE DATABASE "jobzumoDB";
Use:
NAME': 'jobzumodb',
Instead of:
NAME': 'jobzumoDB',

Deployment of Django application using MongoDB on AWS

How to define the settings of the django application for using the mongodb server running on the same instance as that of the django project. I tried it with 127.0.0.1, port 27017 (which I assume is the default port at which the mongodb server runs), in the settings of the django application. I then tried it with the IP address of the aws instance, but with no luck. It always gives me this error:
ConnectionError: You have not defined a default connection
My django project has the following mongo settings.
MONGO_SETTINGS = {
'DB_NAME' : 'spotmentor',
'HOST' : '127.0.0.1',
'PORT' : 27017,
'USERNAME' : '',
'PASSWORD' : ''
}
Then I used the mongoengine connect to establish the connection.
I am importing the above MONGO_SETTINGS as mongoset and
from mongoengine import connect
connect(mongoset.get('DB_NAME'), host = mongoset.get('HOST'), port = mongoset.get('PORT'), username = mongoset.get('USERNAME'), password = mongoset.get('PASSWORD'))
I changed the value of the HOST key to the aws instance public IP and still I got the same ConnectionError.
I have also defined:
DATABASES = {
'default' : {
'ENGINE': 'django.db.backends.dummy',
}
}
How can I resolve this?
mongoengine does not require any extra settings to connect to mongodb. The settings that you have provided must suffice.
I suggest you re-check your installation of mongodb.
Try sudo apt-get remove mongodb
and sudo apt-get install mongodb
This should solve your problem.
Also, you need not define dummy db backend if you are not using sql-databases.

PostgreSQL & Heroku - cannot connect to the database

I'm trying to add a column to a table via my Django app with South but keep getting the following error upon running the python manage.py migrate <app name> command:
conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: could not translate host name "ec2-107-21-99-105.comp
ute-1.amazonaws.com" to address: Temporary failure in name resolution
Does anybody have an idea why this is happening? I'm a newbie to both South AND the PostgreSQL database management system (which Heroku uses), so I am more than a bit confused.
Make sure you have defined your default database in settings.py like this:
DATABASES = {
'default': dj_database_url.config(default=os.environ.get('DATABASE_URL'))
}