それマグで!

知識はカップより、マグでゆっくり頂きます。 takuya_1stのブログ

習慣に早くから配慮した者は、 おそらく人生の実りも大きい。

postgresql

PostgreSQLでtimestamp/now() を文字列にする char キャスト

PostgreSQL でタイムスタンプを文字列として取り出したい char にキャストするときに長さをつける now() の場合 select now()::char(30); 実行結果 sample> select now()::char(30); +--------------------------------+ | now | |-------------------------…

PostgreSQLでランダムなINTを生成する

ランダムなINTがほしい cast の場合はこれ select cast(random()*100 as integer); ::int を使ったキャストを使う場合はこんな感じ floor(random() * 1000 + 1)::int; 参考資料 https://www.postgresql.jp/document/9.4/html/functions-math.html

PostgreSQLにあとからUNIQUE制約を追加する。

PostgreSQL で PostgreSQLにあとからUNIQUE制約を追加する。 ALTER TABLE "TABLE_NAME" add constraint "your_name_of_constraint_key" uniq ("COLUNM_NAMES"); PostgreSQL にあとからUNIQUEを削除する ALTER TABLE "TABLE_NAME" drop constraint "your_name…

postgresqlでクエリのログをとりたい

psql でクエリのログをとりたい log_destination = 'stderr' redirect_stderr = on log_directory = '/work/postgres/logs' log_rotation_age = 86400 #log_parser_stats = off #log_planner_stats = off #log_executor_stats = off log_statement = 'all' …

psql コマンドをWindows使う

PostgreSQLのインストーラーで入れても良いけど,cygwinがあれば,クライアントだけを手軽に導入できた.パッケージをインストール takuya@letsnote:~$ apt-cyg install postgresql-client psql がErrorになる takuya@letsnote:~$ cygcheck psql takuya@lets…

PostgreSQLのpsqlでテーブル定義を確認する方法

MySQLでテーブルだとshow create table でCREATE文を確認できたと思うんです postgresql で show create table をするには pg_dump -U postgres --schema-only my_db --schema-only テーブル定義: をつける --table=テーブル名 テーブルを制限する コマンド…

PostgreSQL対話モード と mysql のShow ***コマンドの比較

PostgreSQL対話モードではMySQLと違ってテーブル情報見るのに、Show SQLコマンドが使えない。そこで、エスケープ文字から始まるコマンドを入れる必要がある。これが覚えにくいんだ。 MySQLのコマンドとPsqlの対比表 mysql psql help \? show tables \dt show…