Date created: 10 Feb 2020
For python we need to use SQL to query the database. We will use the pandas
package to directly read in the results. For more information how to create an SQL query, you can look at https://www.w3schools.com/sql/sql_syntax.asp and/or https://tutorialzine.com/2016/01/learn-sql-in-20-minutes
Note that the access to the database is restricted to read-only, i.e. no new tables or records can be created by the users.
import pandas as pd
import psycopg2
# establish connection using information supplied in documentation
conn = psycopg2.connect(host="10.8.244.31",
database="climate_data",
user="climate_user",
password="meteo_data",
port=5432)
cur = conn.cursor()
# get the metadata
query = """
SELECT * FROM metadata
"""
tbl_meta = pd.read_sql_query(query, conn)
tbl_meta.info()
tbl_meta.head()
# get some sample data from station "deutschnofen" and since 2012
query = """
SELECT * FROM stations_data
WHERE date > '2012-01-01' AND station = 'Deutschnofen'
"""
tbl_data = pd.read_sql_query(query, conn)
tbl_data.info()
tbl_data.head()
# close the connection when finished
cur.close()
conn.close()