Geo*_*los 8 django postgresql google-cloud-platform
这是在setting.py文件中使用Django框架进行本地postgresql连接的默认配置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'LOCAL_DB_NAME',
'USER': 'LOCAL_DB_USER',
'PASSWORD': 'LOCAL_DB_PASS',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Run Code Online (Sandbox Code Playgroud)
如何配置它以使用Google Cloud Postgresql托管数据库?
Geo*_*los 14
首先,您需要设置数据库配置如下所示:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'DATABASE_NAME',
'USER': 'DB_USER_NAME',
'PASSWORD': 'DB_USER_PASS',
# https://console.cloud.google.com/sql/instances
'HOST': '<ip of your google instance>',
'PORT': '5432', #at the moment of this writing google cloud postgresql is using the default postgresql port 5432
'OPTIONS': {
'sslmode': 'verify-ca', #leave this line intact
'sslrootcert': '/your/path/to/server-ca.pem',
"sslcert": "/your/path/to/client-cert.pem",
"sslkey": "/your/path/to/client-key.pem",
}
}
}
Run Code Online (Sandbox Code Playgroud)
从以下网址获取ip/hostname:https://console.cloud.google.com/sql/instances
要创建和下载三个pem文件,您需要访问类似于以下内容的内容:
https://console.cloud.google.com/sql/instances/INSTANCE_NAME/ssl
然后单击"客户端证书"按钮
要实际允许远程连接(如果您在本地运行django进行开发),则需要单击右侧的"授权"选项卡(https://console.cloud.google.com/sql/instances/INSTANCE_NAME/authorization),然后输入您的公共IP或您组织的公共网络.只允许此IP /网络访问.
要实际生成实例并选择postgresql,请生成您需要遵循本教程的用户和密码:https://cloud.google.com/sql/docs/postgres/quickstart
现在常规python manage.py makemigrations --settings=settings.my_settings应该工作得很好
如果您需要使用psql验证连接,则可以在终端中使用此命令进行连接:
psql "sslmode=verify-ca sslrootcert=/your/path/to/server-ca.pem \
sslcert=/your/path/to/client-cert.pem \
sslkey=/your/path/to/client-key.pem \
hostaddr=<ip address of google cloud instance> \
user=<your db username> dbname=<your db name>"
Run Code Online (Sandbox Code Playgroud)
系统将提示您输入密码
请享用!
| 归档时间: |
|
| 查看次数: |
935 次 |
| 最近记录: |