Newer
Older
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 14 13:43:45 2021
export the stationlist to database
@author: geopeter
"""
import psycopg2
import psycopg2.extras
from psycopg2 import sql
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
cfg = configparser.ConfigParser()
cfg.read('config.ini')
assert "POSTGRES" in cfg, "missing POSTGRES in config.ini"
param_postgres = cfg["POSTGRES"]

Clemens Berteld
committed
# Use existing connection to DB "postgres" to create DB "temperatures_berteld_morstein"

Clemens Berteld
committed
def create_db(db_name):
print("Create DB: ", db_name)
connection = psycopg2.connect(dbname='postgres', user=param_postgres["user"], password=param_postgres["password"], host=param_postgres["host"], port=param_postgres["port"])
connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) # Needs to be in AUTOCOMMIT mode for creating database
with connection.cursor() as cursor:

Clemens Berteld
committed
create_db_query = sql.SQL("""CREATE DATABASE {};""".format(db_name))
cursor.execute(create_db_query)
connection.close()

Clemens Berteld
committed
def drop_db(db_name):
if dbexists(db_name):
print("Drop DB: ", db_name)
try:
connection = psycopg2.connect(dbname='postgres', user=param_postgres["user"], password=param_postgres["password"], host=param_postgres["host"], port=param_postgres["port"])

Clemens Berteld
committed
connection.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) # Needs to be in AUTOCOMMIT mode for creating database
with connection.cursor() as cursor:

Clemens Berteld
committed
cursor.execute("DROP DATABASE {};".format(db_name))

Clemens Berteld
committed
except psycopg2.DatabaseError as error:
# do nothing, because test db is clean
print(error.message)
return

Clemens Berteld
committed
def dbexists(db_name):
try:
connection = psycopg2.connect(dbname='postgres', user=param_postgres["user"], password=param_postgres["password"], host=param_postgres["host"], port=param_postgres["port"])
cursor = connection.cursor()
cursor.execute("SELECT datname FROM pg_database WHERE datname LIKE '{}';".format(db_name))
db_exists = cursor.fetchall()[0]
connection.close()
return True
except(Exception, psycopg2.DatabaseError) as error:
# do nothing, because test db is clean
return False
# Connect to DB "postgres" to check for database "temperatures_berteld_morstein"

Clemens Berteld
committed
def check_for_db_existence(station_list, db_name):
print("Checking for database existence")

Clemens Berteld
committed
if dbexists(db_name):
print('DB existing exists')
else:

Clemens Berteld
committed
create_db(db_name)
create_table(station_list, db_name)
# Connect to DB "temperatures_berteld_morstein" to create table "temperatures"

Clemens Berteld
committed
def create_table(station_list, db_name):
df_columns = list(station_list)
columns = ['id INTEGER', 'lon NUMERIC', 'lat NUMERIC', 'country TEXT', 'file TEXT']
for column in df_columns:
if str(column).startswith('19') or str(column).startswith('20'):
columns.append('"{}" NUMERIC'.format(column))
columns_clean = str(columns).strip('[]').replace("'", "")

Clemens Berteld
committed
with psycopg2.connect(database=db_name, user=param_postgres["user"], password=param_postgres["password"], host=param_postgres["host"], port=param_postgres["port"]) as connection:
query = sql.SQL("""CREATE TABLE stations ({});""".format(columns_clean))

Clemens Berteld
committed
def insert_data(station_list, db_name):
with psycopg2.connect(database=db_name, user=param_postgres["user"], password=param_postgres["password"], host=param_postgres["host"], port=param_postgres["port"]) as connection:

Clemens Berteld
committed
if len(station_list) > 0:

Clemens Berteld
committed
df_columns = list(station_list)
# As integers like 2018, 2017, etc. are not possible as column names, double quotes have to be added. This requires some tricks and cleanups
columns = []
for column in df_columns:
columns.append('"' + column + '"')
columns = str(columns).replace('[', '').replace(']', '').replace("'", "").replace('\n', '').replace(' ', '')

Clemens Berteld
committed
station_list = station_list.round(decimals=3)
# create VALUES('%s', '%s",...) one '%s' per column
values = "VALUES({})".format(",".join(["%s" for _ in df_columns]))
# create INSERT INTO table (columns) VALUES('%s',...)
insert_stmt = """INSERT INTO {} ({}) {}""".format('stations', columns, values)

Clemens Berteld
committed
psycopg2.extras.execute_batch(cursor, insert_stmt, station_list.values)

Clemens Berteld
committed
def export(station_list):
check_for_db_existence(station_list, param_postgres['dbName'])
insert_data(station_list, param_postgres['dbName'])