$ createuser -P username
Enter password for user "okumura": hoge
Enter it again: hoge
Shall the new user be allowed to create databases? (y/n) y
Shall the new user be allowed to create more new users? (y/n) n
Ctrl + D
(create user okumura with password 'hoge'; と同じ)
DB作成
$ createdb test
存在するデータベースの名前を一覧表示
$ psql -l
テーブルの作成
test=> CREATE TABLE site(
test=> id int4 not null unique,
test=> name varchar(128) not null,
test=> url varchar(256)
test=> );
not null : データ挿入時に値がないとエラー
unique : テーブル内に同じ値があるとエラー
データの挿入
test=> INSERT INTO site ( id, name, url) VALUES(2,'yahoo',
'http://www.yahoo.co.jp');
データの更新
test=> UPDATE site SET url='none' WHERE name='google';
name が google の url を none に変更
データの削除
test=> INSERT INTO site(id,name,url) VALUES(3,'test','test');
test=> INSERT INTO site(id,name,url) VALUES(3,'test','test');
test=> DELETE FROM site WHERE name='test';
name が test のものを削除
集計(集約)
test=> SELECT count(*) FROM site;
test=> SELECT count(*) FROM site where name='test';
psql のコマンド
\? で調べられる
\d DBに何が存在するか表示
\d Name(テーブル名) テーブルの構造を表示
データベースのバックアップと復元
pg_dump (データベース名) > dump.out
psql -e (データベース名) < dump.out