-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres_query.py
More file actions
49 lines (39 loc) · 1 KB
/
postgres_query.py
File metadata and controls
49 lines (39 loc) · 1 KB
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
import os
import sys
import subprocess
from google.cloud.sql.connector import Connector
import sqlalchemy
# set credentials
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'genotools-02f64a1e10be.json'
# connection variables
project_id = 'genotools'
region = 'us-central1'
instance_name = 'genotools'
INSTANCE_CONNECTION_NAME = f'{project_id}:{region}:{instance_name}'
DB_USER = 'postgres'
DB_PASS = 'genotools'
DB_NAME = 'snp_metrics'
# initialize gcloud sql connector
connector = Connector()
# get connection
def getconn():
conn = connector.connect(
INSTANCE_CONNECTION_NAME,
"pg8000",
user=DB_USER,
password=DB_PASS,
db=DB_NAME
)
return conn
# create and sqlalchemy engine
pool = sqlalchemy.create_engine(
"postgresql+pg8000://",
creator=getconn,
)
# connect
db_conn = pool.connect()
# query and fetch snps table
triple_join_count = db_conn.execute("select count(*) from samples").fetchall()
print(triple_join_count)
# close connection
connector.close()