I've been struggling with the following problem:
I have a MySQL database running on a remote web host. I connect to the MySQL database in my Django app (I use it as the main database). The Django app is running on a Heroku server but I get different data results compared to running it locally.
Am I missing something, or are changes done on Heroku not committed to the database?
MySQL settings:
DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'xxx',
'USER': 'xxx',
'PASSWORD': 'xxx',
'HOST': 'xxx',
'PORT': 'xxx',
}
}
I just see that if you delete the ad-on provided by heroku, it will use the DB you put on the settings of your project
I believe Juan Carlos Hernández is correct. To expand on his answer, Heroku uses it's own database instance, unless you tell it otherwise by pointing the DATABASE_URL to the one you would like to use. Please note that Heroku will prevent you from overwriting the DATABASE_URL in use since that will destroy the existing database. Although tagged as ruby-on-rails, the answers I found here seem relevant.
To summarize, you just have to run
heroku config:add DATABASE_URL=mysql://dbusername:dbpassword#databasehostIP:3306/databasename
heroku config:add SHARED_DATABASE_URL=mysql://dbusername:dbpassword#databasehostIP:3306/databasename
Then do
heroku restart
Or you can change these variables in the Heroku panel.
Related
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?
Currently i am working on a web app with Django and MongoDB (using Djongo).
I have connected the mongodb server succesfully on my pc and everything worked fine but when
i started deploying the project to heroku, it connected to a random postgresql server instead of my
mongoDB server. Heres my DATABASES settings in settings.py:
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'database',
'CLIENT': {
'host': 'mongodb+srv://connectionstring',
'username': 'name',
'password': 'pass'
}
}
}
Any clue why this is happening?
Never mind figured it out (Heroku only allow a few types of databases and auto installs Heroku Postgres).
I am building an app using Django and Postgres. I managed to do migrations and I want to test it. When I test with sqlite everything works fine, but when I run tests with postgres I'm getting this error:
Creating test database for alias 'default'...
Got an error creating the test database: permission denied to create database
I've checked user's permissions and I'm sure that this user have permission to create database.
My database config looks like this:
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
# }
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': '***',
'USER': '***',
'PASSWORD': '***',
'HOST': '****',
'PORT': '****',
}
}
My postgres db is on a server.
My questions are:
What is the right way to config my db and run tests?
Should I be using sqlite for testing?
If so how my code should look like, so I don't have to comment configs?
It looks like your DB user doesn't have permission to create a new database. Please, take a look here. This command-line utility allows you to create a user and set their permissions.
Example:
createuser my_user --createdb -W --username postgres
Note: you are creating user "my_user" on behalf of PostgreSQL admin role which is postgres by default.
Answering your questions:
You may have several configs for different stages, e.g development, testing, production.
You could use both SQLite and Postgres databases for testing purposes to some extent. You should be awarded, though, if your app relies on some specific features available only in Postgres, then using SQLite for testing doesn't make sense. I personally prefer using the same database for all stages. You could also use docker if you don't want to install DB server on your machine.
Local database setting-
`DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}`
main database setting-
`DATABASES = {
'default': {
'ENGINE':'django.db.backends.mysql',
'NAME': '******',
'USER': '******',
'PASSWORD': '********',
'HOST': '******',
'PORT': '****',
}
}`
I did it from the git project
python manage.py dumpdata > all.json . But then I don't know what to do next like how to use this json file to update local database by which commands.
I am very beginner to this. kindly help me solve this problem.
You should always use an RDBMS from the same vendor at both ends. And preferably the same version. Though ORMs such as Django's are supposed to be database agnostic, there are still many subtle differences between different vendor's products. Sqlite for example is the least feature complete of the popular open source databases. If you use sqlite in development, and postgresql in production, you are limiting yourself to the set of features that sqlite has. But postgresql has many more. Right end of sermon.
At the live server do
python manage.py dumpdata > data.json
Then copy the file over to the local machine and do:
python manage.py loaddata data.json
python manage.py migrate
python manage.py loaddata all.json
this command will load entire data to database whatever you specified in settings.py
I'm attempting to run tests on a GIS Django application running PostGIS as a database backend.
When I attempt to run tests, I get the following error:
django.db.utils.ProgrammingError: permission denied to create extension "postgis"
HINT: Must be superuser to create this extension.
The error makes sense. Only admin database users can install extensions since this privilege allows the execution of arbitrary external code. BUT since the test runner has to re-create the database each time the tests are run, Django's database user can't proceed.
Here is my database configuration.
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'my_db',
'USER': 'my_user',
'PASSWORD': 'my_crazy_secure_password',
'HOST': '127.0.0.1',
'PORT': '',
'TEST_NAME': 'test_my_db',
},
}
My solution to this was surprisingly simple, once I figured it out.
Connect to the template1 database, and run CREATE EXTENSION IF NOT EXISTS postgis;. The template1 database is copied when a new database is created, so all new databases will already have the extension installed.