Python/Postgresql – Query Database and present data in a plot

How to query a database using a Python script and present the data on a plot.

import matplotlib.pyplot as plt
import pandas as pd
from pandas import DataFrame
import datetime
import psycopg2
import re
import os

conn = psycopg2.connect("host='' dbname='servers' user='seeker'")
cur = conn.cursor() #Create the cursor
cur.execute("select total_ram_raw, used_ram_raw, time_captured  from server_perf  where hostname='localhost.localdomain'")
rows = cur.fetchall()

df = pd.DataFrame(rows, columns=['TotalRam','UsedRAM', 'Time'])

df.TotalRam = df.TotalRam.astype(float)
df.UsedRAM  = df.UsedRAM.astype(float)

df = pd.read_csv('file.csv', index_col='Time')
df[['TotalRam', 'UsedRAM']].plot()
Share Button