Postgresqlでデータベースとテーブルを作成する

作成したテーブルにアクセス出来ないと思ったら、作ったデータベースに対してテーブルを作成していなかった。 それに30分ぐらい費やしてしまったので基本の所をメモ。

Postgresqlへの接続

psql -U postgres

データベースの作成

create database <db_name>

データベースの選択(MySQLでいうuse

\c db_name

テーブルの作成

create table posts (
  id integer primary key,
  name text not null,
  created_at timestamp not null default current_timestamp,
  updated_at timestamp not null default current_timestamp
);