3

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"?
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
appleLover
  • 14,835
  • 9
  • 33
  • 50
  • 2
    There's no development DB? In any case, the prod database is called "sports", indicated by where it says `database: sports` in the config file. – Dave Newton Mar 14 '14 at 12:31
  • i am trying the command "psql sports" in the root directory, in the db, and config directories, and it says "FATAL: database sports does not exist"... you are right, there is a development DB, sorry i missed that earlier. updating my post now – appleLover Mar 14 '14 at 12:35
  • 1
    Have you run `rake db:create` and `rake db:migrate` to actually create the DB? By default you'll be in the `development` environment, where the DB is called `danwork`. *Where* you type in the command is not relevant; `psql` knows nothing of the Rails environment. – Dave Newton Mar 14 '14 at 12:38
  • If I understand you correctly, this person just created application, but it wasn't run yet, it's just code? In such case you need to create the database (`rake db:create db:migrate`) and then you can connect and check columns – Gregory Witek Mar 14 '14 at 12:39
  • The dev database is called 'danwork' but you're getting an error saying there's no database called "dan", which sounds like you're entering his username where it expects the database name. Can you add the postgres command you're using to access the database to your OP? – Max Williams Mar 14 '14 at 12:39
  • 1
    @arnvald - he's expecting there to be some actual data, so making a new db using `create` won't help. – Max Williams Mar 14 '14 at 12:40
  • With your update, looks like your development db won't exist. To test just replace the development db details with the production db details. If that works, you'll have to perform `rake db:migrate` as Max said – Richard Peck Mar 14 '14 at 12:41

4 Answers4

2

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.

Community
  • 1
  • 1
pillravi
  • 4,035
  • 5
  • 19
  • 33
1
  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
    
Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
0

To access your database using plain PostgreSQL

su username
psql
\db database_name;
OneChillDude
  • 7,856
  • 10
  • 40
  • 79
  • What a roundabout way of doing it. If you're using password auth, simply `psql -U username dbname`. If you're using `peer` auth, `sudo -u username psql dbname`. – Craig Ringer Mar 15 '14 at 01:02
0

To connect with psql, you must specify the database name.

If you're using password auth, simply psql -U username dbname.

If you're using peer auth, sudo -u username psql dbname.

In your case you seem to be using password auth and connecting to the local host, so I suggest:

psql -U dan danwork

and entering the password when prompted. (You can use a ~/.pgpass file to save the password if desired; see the pgpass documentation)

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778