如何在heroku上执行.sql脚本? [英] how to execute a .sql script on heroku?
问题描述
我有一个.sql文件,其中有一堆插入命令,我想在heroku上的postgres数据库上执行。但是我不知道该怎么做: -
如果我有权访问postgres控制台,可以输入以下内容:
psql -h localhost -d database -U username -f datafile.sql
,但似乎heroku不支持这个命令。我尝试过使用
heroku pg:psql
但是不允许我输入文件。
是否有其他选项?
对于诸如种子数据库之类的事情,我建议Richard Brown的回答:使用Rails种子机制或类似脚本耙任务。
也就是说,能够管理sql(原始文件或文件)是一项非常有用的功能,特别适用于诸如简单查找或常规查询等幂等事物。在这种情况下,您可以使用以下任何一种方式执行本地sql:
$ cat file.sql | heroku pg:psql --app app_name
$ echoselect * from table; | heroku pg:psql --app app_name
$ heroku pg:psql --app app_name< file.sql
I have a .sql file with a bunch of insert commands that I want to execute on my postgres database on heroku. But I don't know how to do it:-
If I had access to postgres console I'd type the following:
psql -h localhost -d database -U username -f datafile.sql
but it seems that heroku doesn't support this command. I've tried with
heroku pg:psql
but that doesn't let me input a file.
Are there any other options?
For things like seeding a database, I recommend Richard Brown's answer: you're arguably better off using something like Rails seeds mechanism, or something scripted like a rake task.
That said, being able to pipe sql (raw, or a file) is a useful feature, especially for idempotent things like simple look ups or routine queries. In which case you can execute your local sql with any of the following:
$ cat file.sql | heroku pg:psql --app app_name
$ echo "select * from table;" | heroku pg:psql --app app_name
$ heroku pg:psql --app app_name < file.sql
这篇关于如何在heroku上执行.sql脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!