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?
Related
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.
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.
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.
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