2017-08-09 60 views
0

我想通过django(版本1.11.3)在postgresql中创建表。我输入以下信息settings.pyDjango 1.11.3无法在postgresql中创建表

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安永tyktsyseyk tytyskt YTR ykrtykeyt ykrtytkyrtyktryetyktyewkyweyekyetywekyeyktywek

DEBUG = True 
TEMPLATE_DEBUG = DEBUG 

ADMINS = (
    # ('Your Name', '[email protected]'), 
) 

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, 
     }, 
    } 
} 
+0

Django不会为您创建PostgreSQL数据库...您必须自己创建一个数据库并提供设置凭据,Django将创建表并对其进行操作。 – zaidfazil

+0

此外,它应该是'django.db.backends.postgresql_psycopg2'如果你正在使用ubuntu – zaidfazil

+0

使用'createdb db_name'由@zaidfazil在尝试迁移之前 –

回答

0

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

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

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

现在,你已经通过去你的网站根目录和打字这样做了设置初始表...

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

现在的表应该显示在您的新的数据库。

+0

试过,太兄弟,但没有工作 – srk