1

My requirement is write the csv from postgres table data , then i facing the below issue , please give some tips to resolve this issue.

org.postgresql.util.PSQLException: ERROR: could not open file 

when i use this query

COPY (SELECT * from <table>) To <filename> DELIMITER ',' CSV HEADER";
Dinesh
  • 11
  • 1
  • 2

2 Answers2

2

You appear to be using PgJDBC.

Most likely you want to use the CopyManager, obtained from PGConnection, to COPY to/from the Java client. See this example, and the PgJDBC API docs.

If you instead intend to do COPY to and from the server file system, you must assign appropriate file system permissions so that the user the PostgreSQL server runs as - usually postgres - can read/write the desired locations.

Community
  • 1
  • 1
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
1

Save file where every user can. In this case user postgres.

Copy (Select * From tablename To '/tmp/file.csv' With CSV DELIMITER ',';
Rafal
  • 573
  • 5
  • 11