繁体   English   中英

如何将 postgres 数据库中的所有表下载为 CSV 格式

[英]How to download all tables from a postgres database as CSV format

我想以 CSV 格式从 Postgres 数据库下载所有表。 下面是我用来从特定数据库下载所有表的示例 shell 脚本。

但是在echo "No User/Role name provided set default User?Role postgres." User=postgres fi后脚本没有运行echo "No User/Role name provided set default User?Role postgres." User=postgres fi echo "No User/Role name provided set default User?Role postgres." User=postgres fi命令。

或者任何人请向我建议任何脚本或 SQL 查询以将所有表下载为 CSV 格式。 提前致谢。

#set -x
read -p "Please provide Schema name   " Schema
if [ -z $Schema ]
then
    echo "No Schema name provided set default schema public."
Schema=public
fi
read -p "Please provide Database name  " Db
if [ -z $Db ]
then
    echo "No Database name provided set default database postgres."
Db=postgres
fi
read -p "Please provide Postgres Role/User name  " User
if [ -z $User ]
then
    echo "No User/Role name provided set default User?Role postgres."
User=postgres
fi

echo " Schema Name is-->$Schema   Database Name--> $Db   User Name-->$User "

psql -U $User -h localhost -Atc "select tablename from pg_tables where schemaname='$Schema'" $Db |\
  while read TABLENAME; do
        echo "$TABLENAME"
    psql -U $User -h localhost -c "COPY $Schema.$TABLENAME TO STDOUT WITH CSV HEADER" $Db  > $TABLENAME.csv
  done

似乎您进行了重定向,这使脚本停止。 更改行:

echo " Schema Name is-->$Schema   Database Name--> $Db   User Name-->$User "

成为:

echo " Schema Name is--"'>'"$Schema   Database Name--"'>'" $Db   User Name-->$User "

符号>在 shell 中具有特殊含义,必须进行转义

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM