Skip to content

Instantly share code, notes, and snippets.

View thnery's full-sized avatar
🎸
Playing The Blues

Tácio Nery thnery

🎸
Playing The Blues
View GitHub Profile
@thnery
thnery / postgres-cheatsheet.md
Created July 22, 2018 22:53 — forked from Kartones/postgres-cheatsheet.md
PostgreSQL command line cheatsheet

PSQL

Magic words:

psql -U postgres

Some interesting flags (to see all, use -h):

  • -E: will describe the underlaying queries of the \ commands (cool for learning!)
  • -l: psql will list all databases and then exit (useful if the user you connect with doesn't has a default database, like at AWS RDS)
@thnery
thnery / rails-jsonb-queries.rb
Last active October 5, 2018 13:59 — forked from mankind/rails-jsonb-queries
Rails-5 postgresql-9.6 jsonb queries
"http://stackoverflow.com/questions/22667401/postgres-json-data-type-rails-query"
"http://stackoverflow.com/questions/40702813/query-on-postgres-json-array-field-in-rails"
# payload: [{"kind"=>"person"}]
Segment.where("payload @> ?", [{kind: "person"}].to_json)
# data: {"interest"=>["music", "movies", "programming"]}
Segment.where("data @> ?", {"interest": ["music", "movies", "programming"]}.to_json)
Segment.where("data #>> '{interest, 1}' = 'movies' ")
Segment.where("jsonb_array_length(data->'interest') > 1")