如何在 Django 中从 Sqlite 切换到 Postgres?

2024-01-04

我有一个为 Sqlite 设置的 Django 应用程序并且工作正常。现在我想切换到Postgres。我安装了这个要求:

psycopg2==2.6

我将 settings.py 更改为:

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

BASE_URL = "localhost:8000"


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '#############################################'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'raven.contrib.django.raven_compat',
    'django_extensions',
    'crispy_forms',
    'compressor',
    'rest_framework',

    'accounts',
    'lists',
)

CRISPY_TEMPLATE_PACK = "bootstrap3"

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'lists.context_processors.extra_context',
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'

DATABASES = {                                                                    
    'default': {                                                                 
        'ENGINE': 'django.db.backends.postgresql_psycopg2',                      
        'NAME': 'my_database_name',                                                   
        'USER': 'my_database_user',                                                   
        'PASSWORD': 'my_database_password',                                      
        'HOST': '127.0.0.1',                                                     
        'PORT': '5432',                                                          
    }                                                                            
} 

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATICFILES_DIRS = (
    'bower_components',
    'less',
    'css',
    'scripts',
)

STATICFILES_FINDERS = (
    "django.contrib.staticfiles.finders.FileSystemFinder",
    "django.contrib.staticfiles.finders.AppDirectoriesFinder",
    "compressor.finders.CompressorFinder",
)

STATIC_URL = '/static/'

STATIC_ROOT = BASE_DIR + '/static/'

COMPRESS_PRECOMPILERS = (
    ('text/less', 'lessc {infile} {outfile}'),
)

AUTH_USER_MODEL = "accounts.Account"

问题是当我运行时:

python manage.py syncdb

or

python manage.py migrate

它创建了一个新的 Sqlite 数据库!我在我的本地服务器上,localhost:8000。 Postgres 运行良好,我已经在 postgres 中正确创建了数据库。为什么会发生这种情况以及如何设置我的 Postgres 数据库?

EDIT:

当我从 shell 打印 settings.DATABASES 时,它仍然显示:

{'default': {'ATOMIC_REQUESTS': False,
             'AUTOCOMMIT': True,
             'CONN_MAX_AGE': 0,
             'ENGINE': 'django.db.backends.sqlite3',
             'HOST': '',
             'NAME': '/mysite/db.sqlite3',
             'OPTIONS': {},
             'PASSWORD': '',
             'PORT': '',
             'TEST': {'CHARSET': None,
                      'COLLATION': None,
                      'MIRROR': None,
                      'NAME': None},
             'TIME_ZONE': 'UTC',
             'USER': ''}}

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Django 中从 Sqlite 切换到 Postgres? 的相关文章

随机推荐