Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is my Rails database?

I paid someone to create a program with Ruby on Rails where it scrapes data and puts it in a Postgres database. In the program's directory are the standard Rails folders, application, "bin", "config" and other directories.

I'm trying to see a list of the columns in a table. I think the best, or only way, to do this is to log into the actual database, and print it out. I'm trying to use a "psql" command to log in but it is saying:

psql: FATAL: database "dan" does not exist

I'm not sure where the database is, or how I can find it.

This is what the config/database.yml contains:

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
  1. Where is my database?
  2. How could I find the database on my own using some linux commands, like find . -iname '...'?
  3. How do I log in, and print out all the columns for the table named "games"?
like image 921
appleLover Avatar asked Dec 07 '25 11:12

appleLover


2 Answers

You want rails dbconsole or rails db.

From the 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.

To see the contents of your games table:

~/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>$

If you really want to use psql -U <username> <dbname>, you can find these parameters as described in this answer and paraphrased here:

~/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>$

As you can see, if no username is returned, you won't need the -U flag.

like image 151
pillravi Avatar answered Dec 09 '25 01:12

pillravi


  1. Your database is running on a server independent of Rails (it will be set up by your coder). It looks like your database is on your localhost.
  2. It will be dependent on your server OS.
  3. If you're looking to show column names in your database, you'll be able to do this in the Rails Console:

    $ rails c
    $ Game.column_names
    
like image 36
Richard Peck Avatar answered Dec 09 '25 00:12

Richard Peck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!