-- USAGE: --sudo -u postgres psql -v id_t=103 -v file="'/home/soltex/export/t_103.csv'" < export_sp.sql copy (select "date", well_depth, t_te.id_event, t_te.message_template, arg0 as old_value, arg1 as new_value from t_telemetry_message t_tm left join (select * from t_telemetry_event where id_telemetry = :id_t) t_te on t_te.id_event = t_tm.id_event where t_tm.id_telemetry = :id_t and t_tm.id_event != 40 order by date) to :file DELIMITER ';' CSV header ENCODING 'WIN1251';