Django Database Migration - python

Hi have a django project a full project now I want to migrate to mysql from the default Sqlite3 which is the default database. I am on a Mac OS and I don't know how to achieve this process. Any one with a complete guide on how to make the switch would be appreciated.

Go to your project's settings file and edit DATABASES with proper database connection
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASSWORD',
'HOST': 'localhost',
'PORT': '3306',
}
}
Now open mysql and create database as you give in DATABASE settings.
Go to your project -
./manage makemigrations
./manage migrate
This will create all the tables in the specified database name.

Related

Migrate development PostgreSQL data to Railway

I've been using PostgreSQL in development and populating the database with some data. I deployed the whole app to Railway following these steps.
When I create a new database in the railway production server it will always be a blank database. How can I migrate the data of my development database to Railway production's?
The code I have in my Django's settings.py file is this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'basededatos1',
'USER': 'db_admin',
'PASSWORD': 'mYWZQy65viXG50wohppK.Z%uauhghs.CuB#k}nE45A=nK}Qe1*_g=sJF~*0}nQ^!aMuCL#]1i+i-hvs1N8:0Q9fGCdC1gNGc5g~T',
'HOST': 'localhost',
'PORT': '5432',
}
}
And added this code while following the deployment tutorial:
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
The newly created PostgreSQL database in the Railway production dashboard is a blank, new database with different parameters to the one I had, but can't edit them. What is the way to go for using my development database's data into production?

Can't reference Django database with dj_database_url

I have a Django application using decouple and dj_database_url (it is not on Heroku). If I put my connection information in settings.ini (DB_NAME, DB_USER, DB_PASSWORD, DB_HOST, and DB_PORT) and set up my database connection in settings.py like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': config('DB_NAME'),
'USER': config('DB_USER'),
'PASSWORD': config('DB_PASSWORD'),
'HOST': config('DB_HOST'),
'PORT': 'DB_PORT',
}
}
It works fine.
However, if I put this in my settings.ini:
DATABASE_URL=postgres://johndoe:mypassword#123.456.789.000:5000/blog_db
and reference it like this in my settings.py:
DATABASES = {"default": dj_database_url.config(default=config("DATABASE_URL"))}
It doesn't work. I just get a 500 when I try to run my server. I presume there is something wrong with my syntax. Can anyone point out what I'm doing wrong?
Thanks!

Running two databases on heroku with django

I have two databases that my Django application needs access. One is a shared database owned by a separate app with the Django app only having read access. The second is entirely owned by the Django app.
For local development I am ok but I'm not sure how to configure things so that Heroku uses the second database.
Currently I have the shared database promoted to DATABASE_URL and the secondary database is at HEROKU_POSTGRESQL_BLUE_URL.
In my settings I have:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'main_database_name',
'USER': 'username',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}, 'secondary': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'secondary_database_name',
'USER': 'username',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}
}
Please ask any more questions if you need me to clarify.
Thanks!
In summary, my specific problem is: I don't know how to have Heroku use the HEROKU_POSTGRESQL_BLUE_URL as the "secondary" database.
---Edit----
At the bottom of settings.py:
# Configure Django App for Heroku.
import django_heroku
django_heroku.settings(locals())
This is where the connection is made between my app's default database and Heroku's DATABASE_URL. I still haven't solved the issue but after some troubleshooting help in the comments, I believe the answer will be found in there.
Here is my working solution.
I have a local_settings.py file not tracked by version control. This contains the database settings for the developer's postgres instance.
local_settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'main_database_name',
'USER': 'username',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}, 'secondary': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'secondary_database_name',
'USER': 'username',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}
}
Then in settings.py I try to import the local_settings.py file. If it doesn't exist, I use the django_heroku package to configure everything for Heroku. In my case, I need to pass the keyword argument databases=False since I want to do a more custom configuration for the databases.
settings.py (only the relevant parts)
# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
# These database settings are used for heroku deployments. They are overwritten with local_settings.py in development.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('DATABASE_URL'),
'USER': '',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}, 'secondary': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('SECONDARY_DATABASE_URL'),
'USER': '',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}
}
try:
from local_settings import *
except ImportError as e:
# Configure Django App for Heroku.
import django_heroku
django_heroku.settings(locals(), databases=False)
In order for this to work you would need to set the Heroku env variable SECONDARY_DATABASE_NAME to the url of your database.
To find out the URL of your secondary database run heroku config -a your_app_name. Copy the url and then set a new environment param in heroku by running
heroku config:set SECONDARY_DATABASE_URL=postgres://xxxxxxx -a your_app_Name
There are a lot of choices with how you would handle the ENV variables, I liked this because I can just set it in my staging and production environments and the same settings work for both.

Django migrate tables to new database

I originally had a django project with a single app and all models were defined in that app. The project, when initiated only used the default database. It has now become an unwieldy app that I'm trying to break down into smaller apps. Doing so, I want to use different databases for the different apps. I've setup new databases and a router in the settings.py file. However, I'm confused about how to migrate existing tables to the new databases.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_name',
'USER': 'db_user_name',
'PASSWORD': 'password',
'HOST': 'hostname',
'PORT': '3306',
},
'db2': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_name2',
'USER': 'db_user_name2',
'PASSWORD': 'password2',
'HOST': 'hostname2',
'PORT': '3306',
}
}
I would want an app (e.g. app1) to be moved from default to db2. The router already knows to specify app1 to db2 but running migrations is doing nothing. Any ideas?
#knbk's answer was ultimately correct, except that the solution involved an additional step.
1. python manage.py migrate auth --database=db2
2. python manage.py migrate app1 --database=db2

Python, MySQL, Django, Syncdb gives DataBaseError (1046, No database selected)

I have installed mysql, added it to services to start automatically, however when i try to run the command line interface it says that it cannot find mysql.exe. It asks me to browse for it. I am using windows 10 64 bit and mysql Cluster 7.4. I am following instructions and all they say to do is just enter the instruction to the command line like this manage.py syncdb and that is all that is said on the matter. Thanks in advance for any help that i get.
Have you defined your database in settings.py
https://docs.djangoproject.com/en/1.9/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
where engine is your database engine.

Categories

Resources