Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
68 lines (57 loc) · 2.22 KB

File metadata and controls

68 lines (57 loc) · 2.22 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START functions_sql_postgres]
from os import getenv
from psycopg2 import OperationalError
from psycopg2.pool import SimpleConnectionPool
# TODO(developer): specify SQL connection details
CONNECTION_NAME = getenv(
'INSTANCE_CONNECTION_NAME',
'<YOUR INSTANCE CONNECTION NAME>')
DB_USER = getenv('POSTGRES_USER', '<YOUR DB USER>')
DB_PASSWORD = getenv('POSTGRES_PASSWORD', '<YOUR DB PASSWORD>')
DB_NAME = getenv('POSTGRES_DATABASE', '<YOUR DB NAME>')
pg_config = {
'user': DB_USER,
'password': DB_PASSWORD,
'dbname': DB_NAME
}
# Connection pools reuse connections between invocations,
# and handle dropped or expired connections automatically.
pg_pool = None
def __connect(host):
"""
Helper function to connect to Postgres
"""
global pg_pool
pg_config['host'] = host
pg_pool = SimpleConnectionPool(1, 1, **pg_config)
def postgres_demo(request):
global pg_pool
# Initialize the pool lazily, in case SQL access isn't needed for this
# GCF instance. Doing so minimizes the number of active SQL connections,
# which helps keep your GCF instances under SQL connection limits.
if not pg_pool:
try:
__connect(f'/cloudsql/{CONNECTION_NAME}')
except OperationalError:
# If production settings fail, use local development ones
__connect('localhost')
# Remember to close SQL resources declared while running this function.
# Keep any declared in global scope (e.g. pg_pool) for later reuse.
with pg_pool.getconn().cursor() as cursor:
cursor.execute('SELECT NOW() as now')
results = cursor.fetchone()
return str(results[0])
# [END functions_sql_postgres]
Morty Proxy This is a proxified and sanitized view of the page, visit original site.