PostgreSQL


ログイン・ログアウト

# su postgres
$ psql
$ ¥q

バージョン確認

# su postgres
$ psql
$ select version();

 PostgreSQL 8.1.23 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

(1 row)

PostgreSQL 起動

[postgres@www ~]$ pg_ctl start -D /home/postgres/data -w
-D initdbしたディレクトリ、起動中の場合はps aux | grep postgresで確認可能。
-w 起動もしくは停止処理が終了するのを待ちます。60秒でタイムアウトします。停止処理のデフォルトです。

PostgreSQL停止

[postgres@www ~]$ pg_ctl stop -D /home/postgres/data

PostgreSQL再起動

[postgres@www ~]$ pg_ctl restart -D /home/postgres/data

PostgreSQL設定ファイル再読み込み

[postgres@www ~]$ pg_ctl reload -D /home/postgres/data

ユーザーの確認

[postgres@www ~]$ psql
postgres=# select * from pg_user;
usename  | usesysid | usecreatedb | usesuper | usecatupd |  passwd  | valuntil | useconfig
----------+----------+-------------+----------+-----------+----------+----------+-----------
 postgres |       10 | t           | t        | t         | ******** |          |

ユーザー作成

[postgres@www ~]$ createuser -U postgres -P
Enter name of role to add: hogeuser
Enter password for new role:
Enter it again:
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) n
Shall the new role be allowed to create more new roles? (y/n) n

[postgres@www ~]$ psql
postgres=# select * from pg_user;
 usename  | usesysid | usecreatedb | usesuper | usecatupd |  passwd  | valuntil | useconfig
----------+----------+-------------+----------+-----------+----------+----------+-----------
 postgres |       10 | t           | t        | t         | ******** |          |
 hogeuser |    16390 | f           | f        | f         | ******** |          |
(2 rows) 

データベースの確認

その1
[postgres@www ~]$ psql -l
   Name    |  Owner   | Encoding  | Collation | Ctype |   Access privileges
-----------+----------+-----------+-----------+-------+-----------------------
 postgres  | postgres | SQL_ASCII | C         | C     |
 template0 | postgres | SQL_ASCII | C         | C     | =c/postgres          +
           |          |           |           |       | postgres=CTc/postgres
 template1 | postgres | SQL_ASCII | C         | C     | =c/postgres          +
           |          |           |           |       | postgres=CTc/postgres

その2
[postgres@www ~]$ psql
postgres=# select oid, datname from pg_database;
  oid  |  datname
-------+-----------
     1 | template1
 11866 | template0
 11874 | postgres
(3 rows)
oidは/home/postgres/data/baseの下にある。

データベースの作成

[postgres@www ~]$ createdb -U postgres testdb01;
[postgres@www ~]$ psql -l
                             List of databases
   Name    |  Owner   | Encoding  | Collation | Ctype |   Access privileges
-----------+----------+-----------+-----------+-------+-----------------------
 postgres  | postgres | SQL_ASCII | C         | C     |
 template0 | postgres | SQL_ASCII | C         | C     | =c/postgres          +
           |          |           |           |       | postgres=CTc/postgres
 template1 | postgres | SQL_ASCII | C         | C     | =c/postgres          +
           |          |           |           |       | postgres=CTc/postgres
 testdb01  | postgres | SQL_ASCII | C         | C     |
(4 rows)

データベースの表示

[postgres@www ~]$ psql
postgres=# ¥l
                              List of databases
   Name    |  Owner   | Encoding  | Collation | Ctype |   Access privileges
-----------+----------+-----------+-----------+-------+-----------------------
 postgres  | postgres | SQL_ASCII | C         | C     |
 template0 | postgres | SQL_ASCII | C         | C     | =c/postgres          +
           |          |           |           |       | postgres=CTc/postgres
 template1 | postgres | SQL_ASCII | C         | C     | =c/postgres          +
           |          |           |           |       | postgres=CTc/postgres
 testdb01  | postgres | SQL_ASCII | C         | C     |
(4 rows)

データベースの選択

postgresq# ¥c testdb
You are now connected to database "testdb".

テーブル一覧表示

testdb# ¥dt

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2022-06-30 (木) 01:40:11