SQL 如何在heroku上执行.sql脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15237366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to execute a .sql script on heroku?
提问by Byron Singh
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:-
我有一个 .sql 文件,里面有一堆我想在 heroku 上的 postgres 数据库上执行的插入命令。但我不知道该怎么做:-
If I had access to postgres console I'd type the following:
如果我可以访问 postgres 控制台,我会输入以下内容:
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 不支持此命令。我试过
heroku pg:psql
but that doesn't let me input a file.
但这并不能让我输入文件。
Are there any other options?
还有其他选择吗?
回答by catsby
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.
对于像为数据库做种这样的事情,我推荐 Richard Brown 的回答:可以说最好使用 Rails 种子机制之类的东西,或者像 rake 任务这样的脚本。
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:
也就是说,能够通过管道传输 sql(原始文件或文件)是一个有用的功能,尤其是对于诸如简单查找或常规查询之类的幂等事物。在这种情况下,您可以使用以下任何一种方式执行本地 sql:
$ 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
回答by John Beynon
Why not just use psql?
为什么不直接使用 psql?
If you look at the output of heroku config
you will see the database URLs (DATABASE_URL key) your application is using - if you take this and break them apart in to the correct bits for using with the psql
all will be good.
如果您查看输出,heroku config
您将看到您的应用程序正在使用的数据库 URL(DATABASE_URL 键) - 如果您将其分解为正确的位以便与psql
all一起使用会很好。
eg
例如
DATABASE_URL: postgres://username:password@host:port/dbname
becomes
变成
psql -h host -p port -d dbname -U username -f datafile.sql
回答by Richard Brown
I like updates that are testable and repeatable. When I need to update the database I write a rake task to perform the update; that way you can run it against test first to guarantee the output is correct before running in production.
我喜欢可测试和可重复的更新。当我需要更新数据库时,我会编写一个 rake 任务来执行更新;这样您就可以先针对测试运行它,以确保在生产中运行之前输出是正确的。
You don't mention if this is an initial database load or one run later, but the convention for loading fresh data into a Rails database is to create a db:seed
rake file that you can execute after your db:migrate
task is done.
您没有提到这是初始数据库加载还是稍后运行,但是将新数据加载到 Rails 数据库的约定是创建一个db:seed
可以在db:migrate
任务完成后执行的rake 文件。
See: http://justinfrench.com/notebook/a-custom-rake-task-to-reset-and-seed-your-databaseAnd: http://railscasts.com/episodes/179-seed-data
请参阅:http: //justinfrench.com/notebook/a-custom-rake-task-to-reset-and-seed-your-database以及:http: //railscasts.com/episodes/179-seed-data