How to export PostgreSQL table data to a csv file

  • 7007890
  • 14-Feb-2011
  • 26-Apr-2012

Environment


Novell Sentinel RD
Novell Sentinel Log Manager
PostgreSQL

Situation

Exporting data from a database is a fundamental task while troubleshooting issues on Sentinel RD and Sentinel Log Manager.

Resolution

PostgreSQL has built-in functionality to help you accomplish this task.

1) Switch context to novell user:
su - novell

2) Set shell and environment variables for Sentinel:
Sentinel Log Manager:
. /opt/novell/sentinel_log_mgr/bin/setenv.sh

Sentinel RD:
. /opt/novell/sentinel6_rd_x86-64/bin/setenv.sh

NOTE: there is a <space> between the <dot> and the first <slash>.

3) Login into the database using psql utility:
/opt/novell/sentinel_log_mgr/3rdparty/postgresql/bin/psql -d SIEM -U dbauser

4) Set the path and file to export the data:
SIEM=> \o /tmp/export_jobs.csv

5) Run the query to export:
SIEM=> select * from scheduled_job_detail limit 10;

6) Exit psql utility:
SIEM=> \q