#!/usr/bin/python

print "Content-type: text/html\n\n";

import psycopg2
import os

db_conn = psycopg2.connect(host='localhost', dbname='rmbt', user='rmbt', password='rmbt')
db_cursor = db_conn.cursor()

#It is possible to change path
sql = "COPY (SELECT * FROM test ORDER BY time ASC) TO STDOUT WITH (FORMAT CSV, DELIMITER ';', HEADER)"
with open("/var/lib/tomcat8/webapps/download/data/data.csv", "w+") as file:
    db_cursor.copy_expert(sql, file)

os.chmod("/var/lib/tomcat8/webapps/download/data/data.csv", 0o777)
db_conn.close()

print("Databaza uspesne vygenerovana")
