6

I find something like this: Rails: How to list database tables/objects using the Rails console?

This line is fine:

ActiveRecord::Base.connection.tables

and returns all tables

but

ActiveRecord::Base.connection.table_structure("users")

generate error:

ActiveRecord::Base.connection.table_structure("projects")

I think that

table_structure

is not Postgres method.

How can I list all data from the table in Rails console for Postgres database?

1 Answer 1

13

You can get this information from postgres directly from the command line

psql your_development_database -c "\d"
-- lists all database tables

psql your_development_database -c "\d users"
-- lists all columns for a table; 'users' in this case

If you want to look at Model attributes in the rails console

User.new
User.new.inspect

# or install the awesome_print gem for better output
ap User.new
Sign up to request clarification or add additional context in comments.

2 Comments

don't forget: \q to exit the psql console. just saved you a search ;)
@grooble - the above is using the -c option so it executes the command but does not enter an interactive psql session, no need to \q in that case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.