【PostgreSQL】一連の操作手順実例。インストール、起動、テーブル作成・削除、終了方法。
- PostgreSQLのインストール
- PostgreSQLの起動
- Databaseの作成
- 指定したDBに移動
- テーブルの作成
- テーブルの確認
- テーブルにデータを追加
- データの更新
- 行の削除
- テーブルの削除
- psqlの終了
**▼補足** No5, 7, 8, 9, 10は通常のSQL作業。その他はpostgreSQL専用のコマンド(psql)を使う。
SQLは末尾に;
が必要。psqlは冒頭に¥
をつける(末尾にセミコロンは不要)
## 1. PostgreSQLのインストール
PostgreSQL公式のダウンロードページにいく。
https://www.postgresql.org/download/macosx/
Download the Installerをクリック。
https://www.enterprisedb.com/downloads/postgres-postgresql-downloads
該当するOSとインストールしたいバージョンをクリック。ここではMacの最新版(13.2)を指定。
ダウンロードが終わったらインストーラをダブルクリック。
保存場所を選択。基本的にはデフォルトでOK。
スーパーユーザー(postgres, ルートユーザー)のパスワードを入力。
ポート番号を入力。デフォルトは5432。
言語を選択。
これまでの設定内容を確認して、問題なければNEXT。
インストール開始。
以上でPostgreSQLのインストールは完了。
Stack BuilderというPostgreSQLの追加ツールのインストールウィザードを起動するか確認が表示される。
後からでも追加できるので、チェックを外してFinishすればOK。
## 2. PostgreSQLの起動 `which postgres`でPostgreSQLのパスを調べる。
$ which postgres
/usr/local/bin/postgres
・起動コマンド:postgres -D パス
$ postgres -D /usr/local/var/postgres
-D
postgresプログラムのディレクトリを指定するオプション。
バックグラウンドで起動する場合は以下。
$ postgres -D /usr/local/pgsql/data >logfile 2>&1 &
### (補足)既に起動中の場合 以下が表示される場合は既に起動中。
$ postgres -D /usr/local/var/postgres
2021-04-02 15:38:48.277 JST [2351] LOG: starting PostgreSQL 13.2 on x86_64-apple-darwin19.6.0, compiled by Apple clang version 12.0.0 (clang-1200.0.32.29), 64-bit
2021-04-02 15:38:48.278 JST [2351] LOG: could not bind IPv6 address "::1": Address already in use
2021-04-02 15:38:48.278 JST [2351] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2021-04-02 15:38:48.278 JST [2351] LOG: could not bind IPv4 address "127.0.0.1": Address already in use
2021-04-02 15:38:48.278 JST [2351] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2021-04-02 15:38:48.278 JST [2351] WARNING: could not create listen socket for "localhost"
2021-04-02 15:38:48.278 JST [2351] FATAL: could not create any TCP/IP sockets
2021-04-02 15:38:48.279 JST [2351] LOG: database system is shut down
$ sudo lsof -i:5432
Password:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 130 postgres 7u IPv6 0xe4c8eae1cbec68e1 0t0 TCP *:postgresql (LISTEN)
postgres 130 postgres 8u IPv4 0xe4c8eae1cb44fbb1 0t0 TCP *:postgresql (LISTEN)
lsof
はファイルを開くコマンド。-i:ポート番号
オプションをつけると指定したポート番号で使用中のファイルのみを表示する。
## 2. PostgreSQLへの接続 Postgreの起動コマンド:`psql -U postgres`
-
-U
ユーザー名を指定 - ユーザー名postgresはルートユーザー
$ psql -U postgres
Password for user postgres:
psql (13.2)
Type "help" for help.
postgres=#
psqlとは?
PostgreSQLの略。対話モードに入るためのコマンド。
よく使うオプションは以下。デフォルト(指定がない場合は)カッコの値が適用される。
-
-U
: ユーザー名(PCのユーザー名) -
-d
: データベス名 -
-h
: ホスト名(localhost) -
-p
: ポート番号(5432)
$ psql -U ユーザー名 -d データベース名 -h ホスト名 -p ポート番号
初期のユーザー名はルートのpostgres
なので指定が必要。
## 3. Databaseの作成 ・`create database `
postgres=# create database mydb;
CREATE DATABASE
DBの確認
・\l
postgres-# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
mydb | postgres | EUC_JP | ja_JP.eucJP | ja_JP.eucJP |
postgres | postgres | EUC_JP | ja_JP.eucJP | ja_JP.eucJP |
template0 | postgres | EUC_JP | ja_JP.eucJP | ja_JP.eucJP | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | EUC_JP | ja_JP.eucJP | ja_JP.eucJP | =c/postgres +
| | | | | postgres=CTc/postgres
先ほど作成した、mydbが確認できる。
postgres, template0, template1はデフォルトで存在している。
DB作成時に指定がない場合はtempalte1の仕様をコピーして作成される。
- Encoding: エンコーディング
- Collate: 文字列並び替え順序
- Ctype: 文字分類
ja_JP.eucJPとは?
Linuxの推奨される日本語文字コード(のロケール名称)
## 4. 指定したDBに移動 ・`¥c <データベース名>`
postgres-# \c mydb
You are now connected to database "mydb" as user "postgres".
mydb=#
コマンド入力の前がmydb=#
となり、移動に成功。
## 5. テーブルの作成
・create table <テーブル名> (<カラム名> <型>)
- テーブル名は複数形
# create table users (name text, email text);
CREATE TABLE
## 6. テーブルの確認 ・`¥d`
mydb=# \d
List of relations
Schema | Name | Type | Owner
--------+-------+-------+----------
public | users | table | postgres
(1 row)
デフォルトでpublicが作成されている。
7. テーブルにデータを追加
usersテーブルの中に、nameとemailというカラムがあるので、ここにデータを挿入する。SQLを使用する。
・insert into <テーブル名> values (<カラム1の値>, <カラム2の値>,,,)
mydb=# insert into users values ('yamada', 'yamada@e-mail');
INSERT 0 1
データの確認。
mydb=# select * from users;
name | email
--------+---------------
yamada | yamada@e-mail
(1 row)
データが追加されていることを確認。
## 8. データの更新 ・`update <テーブル名> set <カラム名> = '値'`
mydb=# update users set name = 'tanaka';
UPDATE 1
# 確認
mydb=# select * from users;
name | email
--------+---------------
tanaka | yamada@e-mail
(1 row)
nameカラムがyamadaからtanakaに変更された。
## 9. 行の削除 ・`delete from <テーブル名> where <条件>`
mydb=# delete from users where name = 'tanaka';
DELETE 1
# 確認
mydb=# select * from users;
name | email
------+-------
(0 rows)
## 10. テーブルの削除 ・`drop table <テーブル名>;`
mydb=# drop table users;
DROP TABLE
# 確認1
mydb=# select * from users;
ERROR: relation "users" does not exist
# 確認2
mydb=# \d
Did not find any relations.
## 11. psqlの終了 ・`¥q`
mydb=# \q
$
以上。