我正在学习 python,我想了解数据库部分以及何时设置 postgresql 数据库。
https://docs.djangoproject.com/en/1.9/ref/settings/#databases
所有的值都是必要的吗?
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
特别是 USER
、PASSWORD
、HOST
、PORT
?我们可以在 django settings.py 中创建 USER
和 PASSWORD
值吗?或者这是数据库的实际 USER/PASSWORD
?此外,对于本地主机,HOST 当前是 127.0.0.1
,但是在部署到生产环境时,我是否将其更改为域名 ( http://www.example.com)?还有PORT
,有必要吗?
最佳答案
Yes! all of those information are necessary , there is no way you could connect to the database unless those values are specified.
是! user
和 password
是您的 PostgreSQL 的实际凭据数据库。
关于部署,您应该在生产数据库所在的位置设置正确的 IP/主机。可能是 example.com 或 xxx.xxx.xxx.xxx
我认为您担心安全
(在源代码中泄露您的数据库凭据)如果是这种情况,您可以将您的凭据在像 .env 这样的安全配置文件中并使用 this 库来处理你的配置文件。
关于Python/Django 数据库用户名和密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38664958/