Sync database content with django - django

We have a local sqlite3 and an online mariaDB database and want to sync the content within django 1.10.3.
The settings are:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'online_database',
'USER': 'xxx',
'PASSWORD': 'xxxxx',
'HOST': 'xxx.xxx.xxx.xxx',
},
'local':{
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'local_database'
}
}
With python manage.py migrate --database=local we were able to sync all the model structures but additionally we are interested in syncing the content of the tables. Is there anything implemented in django?

Maybe you can do it using fixtures. Use the following:
python manage.py dumpdata --database online_database <other parameters> > fixture.json
python manage.py loaddata --database local_database <other parameters> path/to/your/fixtures/fixture.json

Related

I'm not able to connect proxy host database in Django

after running
python manage.py runserver
Got the Error port 5432 failed: FATAL: Feature not supported: RDS Proxy currently doesn’t support command-line options.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'DATABASE_NAME',
'USER': 'DATABASE_USER'
'PASSWORD': 'DATABASE_PASSWORD'
'HOST': 'PROXY_HOST',
'PORT': '5432',
'OPTIONS': {
'options': f"-c search_path={DATABASE_SCHEMA}"
},
}
}
After commented OPTIONS -- {DATABASE_SCHEMA}
models already migrated in particular schema but schema is not detected.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'DATABASE_NAME',
'USER': 'DATABASE_USER'
'PASSWORD': 'DATABASE_PASSWORD'
'HOST': 'PROXY_HOST',
'PORT': '5432',
# 'OPTIONS': {
# 'options': f"-c search_path={DATABASE_SCHEMA}"
# },
}
}
so finally getting migrations error because it's pointing to the database not particular schema
You have 178 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): **address, analytics, etc...**
Run 'python manage.py migrate' to apply them.
February 09, 2023 - 15:42:20
Django version 3.2.9, using settings 'MoreProjectAPI.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
it's not connected my schema could any one please suggest how can i fix this issue in root level not in every model level.
hi your server output after commenting looks fine, try migrating first as it suggests!
python manage.py migrate
or
python3 manage.py migrate
or simply
./manage.py migrate
and then run your server

How to migrate to Postgresql?

My Django project on Heroku I want to migrate from SQLite to PostgreSQL. I changed my settings.py to PostgreSQL. On Windows I installed PostgreSQL and psycopg2. I created the database manually.
As I run makemigrations it creates an SQLite database. Why?
After you have run python manage.py makemigrations you need to run python manage.py migrate to apply the data to the new database.
#Dacey you'll need to edit the settings.py file and change the database name (and potentially connection).
Database
https://docs.djangoproject.com/en/4.1/ref/settings/#databases
''' here you use triple quotes to block out your original statement
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'old_sqlite.db',
} }
close your triple quotes '''
Change the above to something like this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'PostGres_New',
'USER': 'postgres',
'PASSWORD': 'postgres',
'PORT':'5432',
'HOST':'localhost',
} }

Django - Using multiple databases, one for each client

I am building a companion web/mobile application for an existing web app. The database is implemented in MySQL. I will be writing an API using Django/Django Rest Framework (DRF).
The application is used by various organizations. The interesting part about the implementation is that each organization has it's own database. In the existing web application, the user enters the database name along with the login credentials.
How to go about implementing this in Django? I am going to have lots of models - and they all need to read from the correct database depending on the current user.
in settings.py
DATABASES = {
'default': {},
'database_one': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db1',
'PORT': '3306',
'HOST': '127.0.0.1',
'USER': 'db1_user',
'PASSWORD': 'db1_pwd'
},
'database_two': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db2',
'PORT': '3306',
'HOST': '127.0.0.1',
'USER': 'db2_user',
'PASSWORD': 'db2_pwd'
}
}
For sync your database, you should do like this:
python manage.py migrate
python manage.py migrate --database=database_one
python manage.py migrate --database=database_two
The first command operates on the default database, and with the --database option you can operate the database you want.
More info. can be found here: https://docs.djangoproject.com/en/2.0/topics/db/multi-db/

django.db.utils.OperationalError: FATAL: database "Path" doesn't exist

I'm newbie with python and django,
Im trying to setting up Django in windows7,
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.path.join(BASE_DIR, 'amour'),
'USER': 'openpg',
'PASSWORD': 'openpgpwd',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
when trying to execute the server: python manage.py runserver
the below error appear :django.db.utils.OperationalError: FATAL: database "Path" doesn't exist
I already install PostgreSql 9.3 and Python 2.7.
You should pass the name of the database, not the filename. So if you created database named "amour" then setting will be:
DATABASES = {
'default': {
...
'NAME': 'amour',
...
}
}

Database configuration in Django

How do I use a database connection URL in setting up database config in Django as opposed to using a dictionary?
Instead of using:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.getenv('DB_NAME'),
'USER': os.getenv('DB_USER'),
'PASSWORD': os.getenv('DB_PASSWORD'),
'PORT': os.getenv('DB_PORT'),
'HOST': os.getenv('DB_HOST')
}
}
I want to use:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgresql://DB_USER:DB_PASSWORD#localhost:5432/DB_NAME'
}
}
This is how I added a database url to one of my django apps.
First, install this package
pip install dj-database-url
Next, add this line to your settings.py in your django project
...
DATABASES['default'] = dj_database_url.config(default=os.getenv('DATABASE_URI'))
...
In place of os.getenv('DATABASE_URI'), you can also explicitly mention your database url
You can refer here to the github repo for more configurations.