Skip to content

PostgresSQL Usage

Maryann Jordan edited this page Feb 7, 2019 · 24 revisions
  • Start the rails console

    docker-compose exec web rails console

  • List contents of table use command Tablename.all in the rails console

    ex. User.all command in rails console will display all of the rows for the User table

    irb(main):001:0> User.all

    output below:

    User Load (1.3ms) SELECT "users".* FROM "users" LIMIT

    => #<ActiveRecord::Relation #<User id: 1, email: "[email protected]", created_at: "2019-01-20 09:34:41", updated_at: "2018-01-20 09:34:41", username: "Joe@RLC", first_name: "Bob", last_name: "Domain", profile_photo: nil, interests: nil, is_admin: false>, #<User id: 2, email: "[email protected]", created_at: "2018-04-27 09:34:41", updated_at: "2018-04-27 09:34:41", username: "Bob@RLC", first_name: "Bob", last_name: "Domain", profile_photo: nil, interests: nil, is_admin: false>

  • List columns of table

    irb(main):004:0> User.new

    output below:

    => #<User id: nil, email: "", created_at: nil, updated_at: nil, username: nil, first_name: nil, last_name: nil, profile_photo: nil, interests: nil, is_admin: false>

  • Display number of rows in table

    irb(main):006:0> User.count

    output below:

    (0.6ms) SELECT COUNT(*) FROM "users"

    => 2

  • Display row with specific userid

    irb(main):006:0> User.find(2)

    output below:

    User Load(0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1

    => #<User id: 2, email: "[email protected]", created_at: "2019-01-20 09:34:41", updated_at: "2018-01-20 09:34:41", username: "Joe@RLC", first_name: "Bob", last_name: "Domain", profile_photo: nil, interests: nil, is_admin: false>