04 Setup Postgres Database Using GCP Cloud SQ
04 Setup Postgres Database Using GCP Cloud SQ
Here are some of the key benefits with respect to Cloud SQL
* Quick and Easy Setup
* Fully Managed by Google Cloud
* Reliable with regular backups and high availability
* Easy migrations using services like Data Migration Service
```shell
psql -h <host_ip> -U postgres -W
```
* Run the below commands to create tables and also populate data in the tables.
```sql
\i data/retail_db/create_db_tables_pg.sql
\i data/retail_db/load_db_tables_pg.sql
```
```sql
\d -- List tables
```python
import psycopg2
conn = psycopg2.connect(
host='34.86.31.197',
database='itversity_retail_db',
user='itversity_retail_user',
password='itversity',
)
cur = conn.cursor()
cur.execute('SELECT * FROM orders LIMIT 10')
cur.fetchall()
```
## Integration of GCP Cloud SQL Postgres with Pandas
Pandas is a powerful Python Data Library which is used to process as well as
analyze the data. It have robust APIs to work with databases.
Here are the steps involved to use Pandas to work with databases like Postgres.
* We need to make sure `pandas`, `psycopg2-binary` as well as `sqlalchemy`
installed using `pip`.
* Pandas uses `sqlalchemy` to interact with database tables based on the connection
url.
* `sqlalchemy` is the most popular ORM to hide the complexity of connecting to the
databases using libraries such as Pandas.
* Here are the examples using Pandas. We first read the data from the files,
process it and then write to Postgres Database using Pandas. We will also read the
data written to Postgres Database Table using Pandas for the validation.
```python
import pandas as pd
columns = ['order_id', 'order_date', 'order_customer_id', 'order_status']
orders = pd.read_csv('data/retail_db/orders/part-00000', names=columns)
daily_status_count = orders. \
groupby(['order_date', 'order_status'])['order_id']. \
agg(order_count='count'). \
reset_index()
help(daily_status_count.to_sql)
daily_status_count.to_sql(
'daily_status_count',
'postgresql://itversity_retail_user:[email protected]:5432/itversity_retail_db
',
if_exists='replace',
index=False
)
help(pd.read_sql)
df = pd.read_sql(
'SELECT * FROM daily_status_count',
'postgresql://itversity_retail_user:[email protected]:5432/itversity_retail_db
'
)
```
Here is the process involved to get secret details as part of the applications.
* Create Secret Manager Client Object
* Get Secret Details
* Use Secret Details (to connect to Databases)
```python
import json
from google.cloud import secretmanager
client = secretmanager.SecretManagerServiceClient()
project_id = 'itversity-rnd'
secret_id = 'retailsecret'
version_id = 1
name = f"projects/{project_id}/secrets/{secret_id}/versions/{version_id}"
payload = json.loads(response.payload.data.decode('utf-8'))
password = payload['password']
```
```python
import pandas as pd
df = pd.read_sql(
'SELECT * FROM daily_status_count',
f'postgresql://itversity_retail_user:{password}@34.86.31.197:5432/
itversity_retail_db'
)
df
```