postgresql-copy

How to export table as CSV with headings on Postgresql?

◇◆丶佛笑我妖孽 提交于 2019-11-26 04:02:24
问题 I\'m trying to export a PostgreSQL table with headings to a CSV file via command line, however I get it to export to CSV file, but without headings. My code looks as follows: COPY products_273 to \'/tmp/products_199.csv\' delimiters\',\'; 回答1: COPY products_273 TO '/tmp/products_199.csv' WITH (FORMAT CSV, HEADER); as described in the manual. 回答2: From psql command line: \COPY my_table TO 'filename' CSV HEADER no semi-colon at the end. 回答3: instead of just table name, you can also write a

Save PL/pgSQL output from PostgreSQL to a CSV file

北城余情 提交于 2019-11-26 00:12:27
问题 What is the easiest way to save PL/pgSQL output from a PostgreSQL database to a CSV file? I\'m using PostgreSQL 8.4 with pgAdmin III and PSQL plugin where I run queries from. 回答1: Do you want the resulting file on the server, or on the client? Server side If you want something easy to re-use or automate, you can use Postgresql's built in COPY command. e.g. Copy (Select * From foo) To '/tmp/test.csv' With CSV DELIMITER ','; This approach runs entirely on the remote server - it can't write to

How to import CSV file data into a PostgreSQL table?

不羁岁月 提交于 2019-11-25 22:32:06
问题 How can I write a stored procedure that imports data from a CSV file and populates the table? 回答1: Take a look at this short article. Solution paraphrased here: Create your table: CREATE TABLE zip_codes (ZIP char(5), LATITUDE double precision, LONGITUDE double precision, CITY varchar, STATE char(2), COUNTY varchar, ZIP_CLASS varchar); Copy data from your CSV file to the table: COPY zip_codes FROM '/path/to/csv/ZIP_CODES.txt' WITH (FORMAT csv); 回答2: If you don't have permission to use COPY