Postgres JSON data type Rails query

For any who stumbles upon this. I have come up with a list of queries using ActiveRecord and Postgres’ JSON data type. Feel free to edit this to make it more clear. Documentation to the JSON operators used below: https://www.postgresql.org/docs/current/functions-json.html. # Sort based on the Hstore data: Post.order(“data->’hello’ DESC”) => #<ActiveRecord::Relation [ #<Post id: 4, … Read more

Creating a PostgreSQL sequence to a field (which is not the ID of the record)

Use CREATE SEQUENCE: CREATE SEQUENCE scores_job_id_seq; — = default name for plain a serial Then add a column default to scores.job_id: ALTER TABLE scores ALTER COLUMN job_id SET DEFAULT nextval(‘scores_job_id_seq’); If you want to bind the sequence to the column (so it is deleted when the column is deleted), also run: ALTER SEQUENCE scores_job_id_seq OWNED … Read more

Can’t find the ‘libpq-fe.h header when trying to install pg gem

It looks like in Ubuntu that header is part of the libpq-dev package (at least in the following Ubuntu versions: 11.04 (Natty Narwhal), 10.04 (Lucid Lynx), 11.10 (Oneiric Ocelot), 12.04 (Precise Pangolin), 14.04 (Trusty Tahr) and 18.04 (Bionic Beaver)): … /usr/include/postgresql/libpq-fe.h … So try installing libpq-dev or its equivalent for your OS: For Ubuntu/Debian systems: … Read more