如何以 CSV 格式从 postgres 数据库下载所有表



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

但是脚本在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 "

符号>在贝壳中具有特殊含义,必须转义

最新更新