使用shell脚本从Heroku Postgres数据库获取数据

zth*_*.nc 3 postgresql heroku

我是这方面的新手。我想编写一个可以从命令行执行的脚本,以在 Heroku 托管的 PostgreSQL 数据库上运行查询。

是的,我有一个具有可执行权限的脚本script.sh,如下所示:

echo "Starting pull from postgres ..."
heroku pg:psql <db> --app <app-name>
\copy (<query>) to 'file.csv' WITH CSV
\q
echo "Done!"
Run Code Online (Sandbox Code Playgroud)

和命令运行良好,但是,一旦 Heroku 启动,脚本就不再echo注入heroku ...命令。只有在我手动关闭 Heroku 应用程序后,它才会注入最后三行。

我知道这是一个 bash 脚本,一旦 Heroku 打开,它不会输入 postgreSQL 命令,但有没有办法做到这一点?

我感觉这可能涉及到连接到 Heroku 并在一行中提交查询——我搜索了Heroku 的文档,但没有看到任何有帮助的内容。

rya*_*ard 5

You can use the --command flag to pass SQL commands to heroku pg:psql along with the server-based COPY and then redirect the output to a file:

echo "Starting pull from postgres ..."
heroku pg:psql <db> --app <app-name> --command "COPY (<query>) TO STDOUT WITH CSV" > file.csv
echo "Done!"
Run Code Online (Sandbox Code Playgroud)