LoginSignup
1
2

More than 3 years have passed since last update.

久々にPostgreSQL触ったら、基本操作すら忘れていたので書いておく

Posted at

:construction: 6年ぶりくらいに触ったらダメでしたw

本当にメモ書き程度ですんません

基本操作

postmasterデーモン

start postmaster デーモンを起動します。
stop postmaster デーモンを停止します。
restart postmaster デーモンを再起動します。
status postmaster デーモンの起動状況を確認します。
reload 設定ファイルを再読込みします。
condstart デーモンが動作しているかを確認後、再起動します。(/var/lock/subsys/postgresql が存在するとき再起動します。)

起動
# /etc/init.d/postgresql start
停止
# /etc/init.d/postgresql stop
再起動
# /etc/init.d/postgresql restart
クラスタ作成
PGLIB=/usr/share/pgsql
PGDATA=/var/lib/pgsql/data
export PGLIB PGDATA
自動起動
# chkconfig postgresql on

データベースユーザの作成

xxxの作成

$ createuser
Enter name of user to add: xxx
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
CREATE USER

データベースユーザの削除

xxxの削除

$ dropuser xxx

データベースの作成

testdb という日本語を EUC で管理するデータベースを作成

$ createdb -E EUC_JP testdb
CREATE DATABASE

ログイン

必要に応じて -h (ホスト)-d (データベース選択) などつけるんだぞ

$ psql -U taro testdb

代表的な操作

\? バックスラッシュコマンドのヘルプを表示します。
\h 利用可能なSQL文を表示します。\h command と実行すれば、その SQLコマンドの書式を表示します。
\i 他のファイルを取り込みます。一連のSQL文を、事前にファイルに記述しておき、\i filename と実行することで、一度に SQL文を実行することができます。
\d データベースの情報を表示します。\d tablename と実行すれば、そのテーブルの定義を表示します。
\dS システムテーブルの一覧を表示します。
\dT ユーザー定義テーブルの一覧を表示します。
\encoding クライアントの現在の文字コードを表示します。\encoding encoding と実行すれば、クライアントの文字コードを設定します。
\l データベースの一覧を表示します。
\z テーブルの一覧をアクセス権限とともに表示します。
\q psql を終了します。

テーブルの作成

testdb=# create table meibo (
testdb(#     name     varchar(30),
testdb(#     age      integer,
testdb(#     address  varchar(80));
CREATE TABLE
testdb=>

INSERT

testdb=# insert into meibo values ('Taro Yamada',27,'1-2-x Shibuya-ku Tokyo');

SELECT

testdb=# select * from meibo;
     name      | age |        address
---------------+-----+------------------------
 Taro Yamada   |  27 | 1-2-x Shibuya-ku Tokyo
(1 rows)

UPDATE

testdb=# update meibo set age = 28 where name = 'Taro Yamada';

SELECT

testdb=# delete from meibo where name = 'Taro Yamada';

DROP

testdb=# drop table meibo;
DROP TABLE

感想


ふー

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2