Django 1.11.3无法在postgresql中创建表

时间:2017-08-09 14:46:43

标签: python django postgresql

我正在尝试通过django(版本1.11.3)在postgresql中创建表。我在settings.py

中输入了以下信息
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'postgres',                      # Or path to database file if using sqlite3.
        'USER': 'postgres',                      # Not used with sqlite3.
        'PASSWORD': 'password',                  # Not used with sqlite3.
        'HOST': 'localhost',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '5432',                      # Set to empty string for default. Not used with sqlite3.
    }
}

但它总是创建SqLite数据库。任何人都可以回答我吗?

我会尝试设置我的settings.py

  

Randon字符串添加我的settings.py:sdfgshrtys yesy ey tyktsyseyk tytyskt ytr ykrtykeyt   ykrtytkyrtyktryetyktyewkyweyekyetywekyeyktywek

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Your Name', 'your_email@example.com'),
)

MANAGERS = ADMINS

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',  # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'db',                         # Or path to database file if using sqlite3.
        'USER': 'postgres',                         # Not used with sqlite3.
        'PASSWORD': 'password',                     # Not used with sqlite3.
        'HOST': 'localhost',                        # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '5432',                             # Set to empty string for default. Not used with sqlite3.
    }
}

ALLOWED_HOSTS = []

TIME_ZONE = 'Asia/Kolkata'

LANGUAGE_CODE = 'en-us'

SITE_ID = 1

USE_I18N = True

USE_L10N = True

MEDIA_ROOT = ''

MEDIA_URL = ''

STATIC_ROOT = ''

STATIC_URL = '/static/'

ADMIN_MEDIA_PREFIX = '/static/admin/'


STATICFILES_DIRS = (

)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#    'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

SECRET_KEY = ''

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
#     'django.template.loaders.eggs.Loader',
)

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

ROOT_URLCONF = 'ex2_postgreSQL.urls'

TEMPLATE_DIRS = (

)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

1 个答案:

答案 0 :(得分:0)

您不应该使用默认的PostgreSQL数据库,而是创建一个新数据库。通过命令行登录数据库,而不是执行>>> CREATE DATABASE yourName

更新 settings.py 以使用该新数据库。

DATABASES = {
    'default': {
        ...
        name: 'yourName'
    }
}

现在您已经通过转到您的网站根目录并输入...来设置初始表格。

>>> python manage.py makemigrations
>>> python manage.py migrate

现在表格应该显示在新数据库中。