![](/img/trans.png)
[英]how to write postgresql script to export data from table to csv file
[英]How to export table data from PostgreSQL (pgAdmin) to CSV file?
我正在使用 pgAdmin 4.3 版,我想將一個表數據導出到 CSV 文件。 我使用了這個查詢
COPY (select * from product_template) TO 'D:\Product_template_Output.csv' DELIMITER ',' CSV HEADER;
但它顯示錯誤
不允許相對路徑使用 COPY 到文件
請問我該如何解決這個問題?
使用 pgAdmin4 GUI 非常容易。
您可以參考https://www.pgadmin.org/docs/pgadmin4/latest/import_export_data.html
使用絕對路徑或 cd 到已知位置,以便您可以忽略該路徑。 例如 cd 進入文檔目錄,然后在那里運行命令。
如果您能夠 cd 進入您的文檔目錄,那么命令將是這樣的:
假設您想從命令行使用 PSQL。 cd ~/Documents && psql -h host -d dbname -U user
\COPY (select * from product_template) TO 'Product_template_Output.csv' DELIMITER ',' CSV HEADER;
結果將是您當前工作目錄(文檔文件夾)中的Product_template_Output.csv 。
再次使用psql 。
您必須刪除雙引號:
COPY (select * from product_template) TO 'D:\Product_template_Output.csv'
DELIMITER ',' CSV HEADER;
試試這個命令:
COPY (select * from product_template) TO 'D:\Product_template_Output.csv' WITH CSV;
在 PgAdmin 中,導出選項在文件菜單中可用。執行查詢,然后我們可以在“輸出”窗格中查看數據。 單擊菜單 FILE -> EXPORT from query window。
PSQL導出數據
COPY noviceusers(code, name) FROM 'C:\noviceusers.csv' DELIMITER ',' CSV HEADER;
https://www.novicetechie.com/2019/12/export-postgresql-data-in-to-excel-file.html供參考。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.