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?
Use the \dt or \dt+ command in psql to show tables in a specific database. Use the SELECT statement to query table information from the pg_catalog.
You can use rails dbconsole to view the database that your rails application is using. It's alternative answer rails db . Both commands will direct you the command line interface and will allow you to use that database query syntax.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With