简体   繁体   English

如何通过Django框架连接到Google Cloud Postgresql?

[英]How do you connect to Google Cloud Postgresql via Django framework?

This is the default configuration one would have for a local postgresql connection using Django framework in setting.py file: 这是在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',
    }
}

How to configure it to work with Google Cloud Postgresql managed database? 如何配置它以使用Google Cloud Postgresql托管数据库?

First of all you need to set the database configuration look like so: 首先,您需要设置数据库配置如下所示:

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",
        }
    }
}

Get ip/hostname from: https://console.cloud.google.com/sql/instances 从以下网址获取ip / hostname: https//console.cloud.google.com/sql/instances

To create and download the three pem files you need to visit something similar to this: 要创建和下载三个pem文件,您需要访问类似于此的内容:
https://console.cloud.google.com/sql/instances/INSTANCE_NAME/ssl https://console.cloud.google.com/sql/instances/INSTANCE_NAME/ssl
And then click Client Certificate Button 然后单击客户端证书按钮

To actually allow remote connection (if you are running django locally for development) then you need to click the AUTHORIZATION tab at the right ( https://console.cloud.google.com/sql/instances/INSTANCE_NAME/authorization ) and then enter your public ip or the public network of your organization. 要实际允许远程连接(如果您在本地运行django进行开发),则需要单击右侧的“授权”选项卡( https://console.cloud.google.com/sql/instances/INSTANCE_NAME/authorization ),然后输入您的公共IP或您组织的公共网络。 Only this ip/network will be allowed access. 只允许此IP /网络访问。

To actually generate the instance along with choosing postgresql, generating the user and the password you need to follow this tutorial: https://cloud.google.com/sql/docs/postgres/quickstart 要实际生成实例并选择postgresql,请生成您需要遵循本教程的用户和密码: https//cloud.google.com/sql/docs/postgres/quickstart

Now the regular python manage.py makemigrations --settings=settings.my_settings should work just fine 现在常规的python manage.py makemigrations --settings=settings.my_settings应该可以正常工作


In case you need to verify the connection by using psql then you can connect using this command in terminal: 如果您需要使用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>"

you will be prompted for a password 系统将提示您输入密码

Enjoy! 请享用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM