@@ -66,27 +66,25 @@ def init_tcp_connection_engine(db_config):
66
66
# [START cloud_sql_postgres_sqlalchemy_create_tcp]
67
67
# Remember - storing secrets in plaintext is potentially unsafe. Consider using
68
68
# something like https://cloud.google.com/kms/ to help keep secrets secret.
69
- db_user = os .environ . get ( "DB_USER" )
70
- db_pass = os .environ . get ( "DB_PASS" )
71
- db_name = os .environ . get ( "DB_NAME" )
72
- db_host = os .environ . get ( "DB_HOST" )
69
+ db_user = os .environ [ "DB_USER" ]
70
+ db_pass = os .environ [ "DB_PASS" ]
71
+ db_name = os .environ [ "DB_NAME" ]
72
+ db_host = os .environ [ "DB_HOST" ]
73
73
74
- db_host_parts = db_host .split (":" )
75
-
76
- # Extract host and port from db_host socket address
77
- db_host = db_host_parts [0 ]
78
- db_port = int (db_host_parts [1 ])
74
+ # Extract host and port from db_host
75
+ host_args = db_host .split (":" )
76
+ db_hostname , db_port = host_args [0 ], int (host_args [1 ])
79
77
80
78
pool = sqlalchemy .create_engine (
81
79
# Equivalent URL:
82
80
# postgres+pg8000://<db_user>:<db_pass>@<db_host>:<db_port>/<db_name>
83
81
sqlalchemy .engine .url .URL (
84
82
drivername = "postgres+pg8000" ,
85
- username = db_user ,
86
- password = db_pass ,
87
- host = db_host ,
88
- port = db_port ,
89
- database = db_name
83
+ username = db_user , # e.g. "my-database-user"
84
+ password = db_pass , # e.g. "my-database-password"
85
+ host = db_host , # e.g. "127.0.0.1"
86
+ port = db_port , # e.g. 5432
87
+ database = db_name # e.g. "my-database-name"
90
88
),
91
89
# ... Specify additional properties here.
92
90
# [END cloud_sql_postgres_sqlalchemy_create_tcp]
@@ -102,25 +100,25 @@ def init_unix_connection_engine(db_config):
102
100
# [START cloud_sql_postgres_sqlalchemy_create_socket]
103
101
# Remember - storing secrets in plaintext is potentially unsafe. Consider using
104
102
# something like https://cloud.google.com/kms/ to help keep secrets secret.
105
- db_user = os .environ . get ( "DB_USER" )
106
- db_pass = os .environ . get ( "DB_PASS" )
107
- db_name = os .environ . get ( "DB_NAME" )
103
+ db_user = os .environ [ "DB_USER" ]
104
+ db_pass = os .environ [ "DB_PASS" ]
105
+ db_name = os .environ [ "DB_NAME" ]
108
106
db_socket_dir = os .environ .get ("DB_SOCKET_DIR" , "/cloudsql" )
109
- cloud_sql_connection_name = os .environ . get ( "CLOUD_SQL_CONNECTION_NAME" )
107
+ cloud_sql_connection_name = os .environ [ "CLOUD_SQL_CONNECTION_NAME" ]
110
108
111
109
pool = sqlalchemy .create_engine (
112
110
# Equivalent URL:
113
111
# postgres+pg8000://<db_user>:<db_pass>@/<db_name>
114
112
# ?unix_sock=<socket_path>/<cloud_sql_instance_name>/.s.PGSQL.5432
115
113
sqlalchemy .engine .url .URL (
116
114
drivername = "postgres+pg8000" ,
117
- username = db_user ,
118
- password = db_pass ,
119
- database = db_name ,
115
+ username = db_user , # e.g. "my-database-user"
116
+ password = db_pass , # e.g. "my-database-password"
117
+ database = db_name , # e.g. "my-database-name"
120
118
query = {
121
119
"unix_sock" : "{}/{}/.s.PGSQL.5432" .format (
122
- db_socket_dir ,
123
- cloud_sql_connection_name )
120
+ db_socket_dir , # e.g. "/cloudsql"
121
+ cloud_sql_connection_name ) # i.e "<PROJECT-NAME>:<INSTANCE-REGION>:<INSTANCE-NAME>"
124
122
}
125
123
),
126
124
# ... Specify additional properties here.
0 commit comments