Home > AI > Uncategorized

postgresql turn all tables in a .sql file into csv

第一步,加载sql文件

>> psql
>> \i backup.sql

第二步,创建.pgpass 文件,以免每执行命令就需要输入密码,内容如下

>> vi .pgpass
>>
localhost:port:database:username:password
>> chmod 600 .pgpass

其中用这个命令查看相关信息

>> psql
>> \conninfo
You are connected to database "u6849956" as user "u6849956" on host "postgresql.csitlabs" (address "150.203.24.39") at port "5432".
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)

第三步,创建脚本并执行

SCHEMA="public"
DB="u6849956"

psql -Atc "select tablename from pg_tables where schemaname='$SCHEMA'" | \
  while read TAB; do
    PGPASSFILE=".pgpass" psql -c "\\COPY $SCHEMA.$TAB TO '$TAB.csv' CSV HEADER;"
  done

最后,打包,下载

>> zip -r my.zip *

 

Related posts:

Leave a Reply