我花钱请人用 Ruby on Rails 创建了一个程序,它可以抓取数据并将其放入 Postgres 数据库中。程序目录下有标准的Rails文件夹、application、“bin”、“config”等目录。
我正在尝试查看表中列的列表。我认为最好的或唯一的方法是登录到实际的数据库,然后打印出来。我正在尝试使用“psql”命令登录,但它显示:
psql: FATAL: database "dan" does not exist
我不确定数据库在哪里,也不知道如何找到它。
这是 config/database.yml 包含的内容:
development:
adapter: postgresql
database: danwork
pool: 5
timeout: 5000
encoding: unicode
username: dan
password: supersecretpassword
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: postgresql
database: sports
pool: 5
timeout: 5000
encoding: unicode
username: namename
password: sports_db
- 我的数据库在哪里?
- 我如何使用一些 linux 命令(如
find )自行查找数据库。 -iname '...'
? - 如何登录并打印名为“games”的表的所有列?
最佳答案
你想要rails dbconsole
或 rails db
.
来自Rails Guide :
rails dbconsole
figures out which database you’re using and drops you into whichever command line interface you would use with it (and figures out the command line parameters to give to it, too!). It supports MySQL, PostgreSQL, SQLite and SQLite3.You can also use the alias “db” to invoke the dbconsole:
rails db
.
查看您的 games
的内容表:
~/Documents/workspace/<project-dir>$ rails db
psql (9.6.3)
Type "help" for help.
development=# select * from games;
id | date | description | city | state
----+------+-------------+----------------------+-------
1 | | | Boston | MA
2 | | | Seattle | WA
(2 rows)
development=# \q
~/Documents/workspace/<project-dir>$
如果你真的想使用psql -U <username> <dbname>
,您可以找到这些参数,如 this answer 中所述并在这里解释:
~/Documents/workspace/<project-dir>$ rails c
Loading development environment (Rails 4.1.4)
2.3.1 :001 > Rails.configuration.database_configuration[Rails.env]
=> {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "database"=>"development"}
2.3.1 :002 > exit
~/Documents/workspace/<project-dir>$ psql development
psql (9.6.3)
Type "help" for help.
development=# \q
~/Documents/workspace/<project-dir>$
如您所见,如果没有返回用户名,您将不需要 -U
旗帜。
关于ruby-on-rails - 我的 Rails 数据库在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22404961/