首先:抱歉这个副本,但其他与此问题相关的类似问题总是告诉同样的方法来解决这个问题,但是对我来说不起作用。我试图将数据库从sqlite迁移到postgresql ...所以我输入:
sudo -u postgres psql
postgres=# ALTER USER postgres WITH PASSWORD 'newpassword';
并且输出返回:" ALTER ROLE "
但是当我输入" python manage.py migrate "我总是收到同样的错误:
django.db.utils.OperationalError:FATAL:密码验证 用户"道格拉斯"
失败了
settings.py:
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'site_.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',
],
},
},
]
WSGI_APPLICATION = 'site_.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
"""
DATABASES = {
#'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
#}
'default': dj_database_url.config(default='postgres://localhost:5432/postgres_db_name'),
}
"""
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'douglas_db',
'USER': 'douglas',
'PASSWORD': 'vamointer',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators
#DATABASES = {'default': dj_database_url.config(default='postgres://127.0.0.1:5432/postgres_db_name')}
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/
LANGUAGE_CODE = 'pt-br'
TIME_ZONE = 'America/Sao_Paulo'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
ps:当我运行' ALTER USER postgres WITH PASSWORD '我把相同的密码放在settings.py中。
谢谢!
答案 0 :(得分:8)
您正在运行的SQL与您尝试使用的用户不匹配。
如果不存在,则需要create the user
CREATE USER douglas WITH PASSWORD 'vamointer';
或如果确实存在,请改为更改用户密码。
ALTER USER douglas WITH PASSWORD 'vamointer';
一旦你做完了,你应该有更多的运气。您可能还需要assign permissions to that user。
答案 1 :(得分:3)
如果您像我一样头脑呆滞,并且在settings.py的Django数据库配置中使用“ USERNAME”而不是“ USER”,请确保将其更改为“ USER”,否则您将看到相同的错误。希望这可以帮助像我这样的人。
答案 2 :(得分:2)
postgresql中的特殊字符在执行时会转换为不同的字符。确保您的密码中没有特殊字符(#,$等)。
如果这样做,请按如下所示更改PostgreSQL密码:
var list = sequelize.define("registry", {
//Item column set to only accept alphanumeric
item: {
type: Sequelize.STRING,
validate: {
isAlphanumeric: true,
}
},
//Quantity column set to only accept alphanumeric
quantity: {
type: Sequelize.INTEGER,
validate: {
isAlphanumeric: true,
}
},
});
return list;
};
//Setup for "guest" table
module.exports = function (sequelize, Sequelize) {
var guests = sequelize.define("guest", {
//Guest name column set to only accept letters
guest_name: {
type: Sequelize.STRING,
validate: {
isAlpha: true,
}
},
//Email column set to only accept email format (test@gmail.com)
email: {
type: Sequelize.STRING,
validate: {
isEmail: true,
}
},
//Phone number column set to only accept 10 numbers
phone_number: {
type: Sequelize.STRING,
validate: {
isNumeric: true,
max: 10,
min: 10,
}
},
});
return guests;
};
//Setup for "main_event" table
module.exports = function (sequelize, Sequelize) {
var events = sequelize.define("main_event", {
//Event name column set to only accept alphanumeric
event_name: {
type: Sequelize.STRING,
validate: {
isAlphanumeric: true,
}
},
//Event date column
//********THIS MIGHT CHANGE DEPENDING ON THE FORMAT*********
event_date: {
type: Sequelize.DATE,
},
//Phone number column set to only accept 10 numbers
//********THIS MIGHT CHANGE DEPENDING ON THE FORMAT*********
event_time: {
type: Sequelize.TIME,
},
});
return events;
};
确保您不要忘记Postgresql命令末尾的sudo -u postgresql psql
postgresql=#alter yourusername with password
'set_new_password_without_special_character';
。
然后运行;
,它应该可以工作!
答案 3 :(得分:1)
就我而言,我不得不改变
'ENGINE': 'django.db.backends.postgresql_psycopg2',
到
'ENGINE': 'django.db.backends.postgresql',
希望它对某人有帮助!
答案 4 :(得分:0)
您可以尝试以下方法:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'telusko',
'USER': 'postgres', #in place we should always be write USER only if we write USENAME then it will give error.
'PASSWORD': '1234',
'HOST':'localhost',
}}
答案 5 :(得分:0)
您的PostgreSQL服务器也可能没有运行。请运行下一个命令,以检查postgres是否正在运行:
sudo service postgresql status
如果没有,请使用以下命令运行它:
sudo service postgresql start
此外,您的设置中的端口可能不正确。要检查Postgres在哪里运行,请使用:
sudo netstat -plunt |grep postgres
在Django设置的PORT
配置中更新DATABASE
后
答案 6 :(得分:0)
发生这种情况的另一个原因是,如果您运行的Postgres版本有冲突。我有一个在docker中运行,另一个在我的系统中运行。
systemctl stop postgresql可以停止系统版本并清除它,如果您只想使用docker版本的话。