happiness (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) (^_^) happiness

PostgreSQL

【PostgreSQL】datetime型変換に苦しむ

posts table column: posted_at: timestamp without timezone型 DATE(posts.posted_at AT TIME ZONE 'JST') AS posted_atするといいみたい [https://qiita.com/kymmt90/items/181df6bf3d3399f299a8:embed:cite]

【PostgreSQL】ユーザーが連続で記録した日数ランキングのsql

概要 ストリークを出すために必要。 railsでありがちなテーブル構成で考える。 テーブル - users - id - posts - user_id - created_at::datetime アソシエーション user has_many posts SQL -- 重複した記録日を一つにする WITH created_at_and_user_ids AS…

【PostgreSQL】PG::ConnectionBad:の対処brewでインストールした場合

エラー内容 PG::ConnectionBad: could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? could not connect to server: Connection refused Is the server runn…

【PostgreSQL】alter default privlideges~のドキュメントが読めなかったのでハマった

シチュエーション PostgreSQL 何も権限持っていない to-reference-userがいる テーブル作成権限持っているtable-craeting-userがいる to-reference-userに永続的に読み取りさせたいスキーマ(テーブル群)がある。 ※スキーマは、publicとかのことで、テーブル…

belongs_to関係のレコードがnullか確認する方法

構造 subcategory belongs_to category サブカテゴリは、category_idを持つ 外部キー制約はない SQL SELECT * FROM subcategories AS s LEFT OUTER JOIN categories AS c ON c.id = s.category_id WHERE c.id IS NULL; シチュエーション railsとかでスピード…

brewのpostgresql起動メモ

$ brew services start postgresql $ createdb test_dbコマンドを打つ(test_dbじゃなくてもよい) $ psql -d test_dbする(すでにpublicスキーマができている) userは、OSのユーザー名、パスなし create table (カラムたち);をしてテーブル作る \dtで確認 見終…