问题
I've recently migrated my postgresql database to a remote server. It used to be on the same machine where I ran Django. I decided to make the move to lighten the load on the machine.
I ran into some trouble however. I did a syncdb and the tables synced up the remote database. I checked to see if the tables did indeed sync and they we're there. Inside the tables there is no data, I have yet to populate them. However, when I access the Django admin page, it's somehow populated with the old data from the local database.
I find this behavior extremely strange. I decided to delete the local database and I got the following error:
FATAL: database "django_db" does not exist
This is weird because the tables are all on the remote database. Both the local and the remote database are called django_db. Could that be the problem?
This is what I have in my settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'django_db', # Or path to database file if using sqlite3.
'USER': 'django_login', # Not used with sqlite3.
'PASSWORD': 'password', # Not used with sqlite3.
'HOST': 'XX.XXX.X.XX', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '5432', # Set to empty string for default. Not used with sqlite3.
},
}
回答1:
I figured it out, I actually had to restart the server that I was working on. It seems, restarting the HTTP server alone won't do that job. The previously compiled code, the .pyc files will linger around unless the server is restarted!
回答2:
You will need to update your settings.py file to point to your new database. This is controlled by the DATABASES
dictionary. Also, as an added tip, I would recommend that you update your system and use dj_database_url
DATABASES = {'default': dj_database_url.config()}
This will keep your database password out of version control. You then just need to create an ENV VAR named DATABASE_URL that contains the information to for your new database. See the documentation or google for more information.
来源:https://stackoverflow.com/questions/17192719/django-keeps-referring-to-the-local-database-instead-of-the-remote-database