簡體   English   中英

我正在使用bash和psql將postgresql數據寫入文件。 我需要將列標題寫入輸出文件

[英]I am writing postgresql data to file using bash and psql. I need the column headers written to the output file

腳本:

#!/bin/bash/
PGPASSWORD='*******' psql -h host_name server_name user_name -t -A -F "," -c "select b.id AS booking_id, b.hotel_id as hotel_id, b.invoice_no as invoice_no, b.guest_id as guest_id from bookings b" > /home/ubuntu/scripts/food_suggest/automate/pgsql_data.csv;

電流輸出:

12345, 1111, ABCD1234, A111 
12346, 1112, ABCD1235, A112 ...

要求的輸出:

booking_id, hotel_id, invoice_no, guest_id
12345, 1111, ABCD1234, A111
12346, 1112, ABCD1235, A112
...

我使用簡單的COPY命令從bash + psql進行操作FORCE QUOTE如果您有逗號或字符串中的字符, 使用params CSV HEADERFORCE QUOTE否則就可以了。

對於json導出,我使用COPY (select row_to_json(...沒有CSV參數。兩者都很好。我將其用於從PG導出到Bigquery。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM