Postgresql

將 Postgres 表導出為 json

  • February 19, 2022

有沒有辦法將 postgres 表數據作為 json 導出到文件中?我需要逐行輸出,例如:

{'id':1,'name':'David'}
{'id':2,'name':'James'}
...

編輯:postgres 版本:9.3.4

在這裡嘗試了解PostgreSQL和的基本介紹JSON

另外,PostgreSQL 文件也很不錯,所以在這裡試試。查看pretty_bool選項。

您最初的問題是“有沒有辦法將 postgres 表數據導出為JSON”。你想要這種格式

{'id':1,'name':'David'}
{'id':2,'name':'James'}
...

我沒有執行的實例,PostgreSQL所以我下載、編譯並安裝了 9.4。

為了回答這個問題,我首先CREATE編輯了一張表 (fred)

CREATE TABLE fred (mary INT, jimmy INT, paulie VARCHAR(20));

INSERT INTO fred VALUES (2,    43, 'asfasfasfd'      );
INSERT INTO fred VALUES (3,   435, 'ererere'         );
INSERT INTO fred VALUES (6, 43343, 'eresdfssfsfasfae');

然後,檢查:

test=# select * from fred;

mary | jimmy |      paulie      
------+-------+------------------
   2 |    43 | asfasfasfd
   3 |   435 | ererere
   6 | 43343 | eresdfssfsfasfae

然後我發出了這個命令

test=# COPY (SELECT ROW_TO_JSON(t) 
test(# FROM (SELECT * FROM fred) t) 
test-# TO '/paulstuff/sware/db/postgres/inst/myfile';
COPY 3
test=# 

然後我退出 psql 並列出文件 myfile.

test=# \q
[pol@polhost inst]$ more myfile 
{"mary":2,"jimmy":43,"paulie":"asfasfasfd"}
{"mary":3,"jimmy":435,"paulie":"ererere"}
{"mary":6,"jimmy":43343,"paulie":"eresdfssfsfasfae"}
[pol@polhost inst]$

(您可以嘗試從

COPY (SELECT ROW_TO_JSON(t, TRUE)  -- <-- Note addition of "TRUE" here!

閒暇時)。

@offby1 指出輸出(雖然對應於 OP 的問題)不正確JSON。@EvanCarroll 指出這\o也是一種輸出到文件的方式,所以我在這個語句中結合了這兩個小問題的解決方案(在此處的幫助下):

test=# \o out.json
test=# SELECT array_to_json(array_agg(fred), FALSE) AS ok_json FROM fred;
                                    -- <-- "TRUE" here will produce plus
                                       ("+) signs in the output. "FALSE"
                                       is the default anyway.
test=# \o

給出:

[pol@polhost inst]$ more out.json 
                                                                  ok_json                                                                    
----------------------------------------------------------------------------------------------------------------------------------------------
[{"mary":2,"jimmy":43,"paulie":"asfasfasfd"},{"mary":3,"jimmy":435,"paulie":"ererere"},{"mary":6,"jimmy":43343,"paulie":"eresdfssfsfasfae"}]
(1 row)
[pol@polhost inst]$ 

最後,@ AdamGent\在他的文章中提到了反斜杠 ( ) 問題。這有點棘手,但無需借助查詢後處理即可。瞧:

INSERT INTO fred VALUES (35, 5, 'wrew\sdfsd');
INSERT INTO fred VALUES (3, 44545, '\sdfs\\\sfs\\gf');

並因此使用 REGEXP_REPLACE(注意演員表 ::TEXT)刪除了多餘的黑斜線。

test=# \o slash.json
test=# SELECT REGEXP_REPLACE(ROW_TO_JSON(t)::TEXT, '\\\\', '\\', 'g') 
test=# FROM (SELECT * FROM fred) AS t;  -- I found that using a CTE was helpful for legibility
test=# \o
test=# \q

給出:

[pol@polhost inst]$ more slash.json 
                   regexp_replace                    
------------------------------------------------------
{"mary":2,"jimmy":43,"paulie":"asfasfasfd"}
{"mary":3,"jimmy":435,"paulie":"ererere"}
{"mary":6,"jimmy":43343,"paulie":"eresdfssfsfasfae"}
{"mary":35,"jimmy":5,"paulie":"wrew\sdfsd"}
{"mary":3,"jimmy":44545,"paulie":"\sdfs\\\sfs\\gf"}
(5 rows)
[pol@polhost inst]$ 

(ps 至於@Zoltán 的評論 - 這可能是一個版本的東西 - 無法重現!)。

引用自:https://dba.stackexchange.com/questions/90482