Database tips for working with Rails Postgres - brownfield-team/anacapa-github-linker GitHub Wiki

Getting the SQL prompt (Postgres)

rails dbconsole

Listing all of the tables

\dt

Listing the columns in a particular table

\d tablename

E.g.

anacapa-github-linker_development=# \d Users;
                                              Table "public.users"
         Column         |            Type             | Collation | Nullable |              Default              
------------------------+-----------------------------+-----------+----------+-----------------------------------
 id                     | bigint                      |           | not null | nextval('users_id_seq'::regclass)
 name                   | character varying           |           |          | 
 username               | character varying           |           |          | 
 uid                    | character varying           |           |          | 
 provider               | character varying           |           |          | 
 created_at             | timestamp without time zone |           | not null | 
 updated_at             | timestamp without time zone |           | not null | 
 email                  | character varying           |           | not null | ''::character varying
 encrypted_password     | character varying           |           | not null | ''::character varying
 reset_password_token   | character varying           |           |          | 
 reset_password_sent_at | timestamp without time zone |           |          | 
 remember_created_at    | timestamp without time zone |           |          | 
 sign_in_count          | integer                     |           | not null | 0
 current_sign_in_at     | timestamp without time zone |           |          | 
 last_sign_in_at        | timestamp without time zone |           |          | 
 current_sign_in_ip     | character varying           |           |          | 
 last_sign_in_ip        | character varying           |           |          | 
Indexes:
    "users_pkey" PRIMARY KEY, btree (id)
    "index_users_on_email" UNIQUE, btree (email)
    "index_users_on_reset_password_token" UNIQUE, btree (reset_password_token)
Referenced by:
    TABLE "roster_students" CONSTRAINT "fk_rails_ce9b706482" FOREIGN KEY (user_id) REFERENCES users(id)

anacapa-github-linker_development=# 

Finding a specific record

select * from Users where username='pconrad';

Rails Console

rails c