LoginSignup
0
0

More than 1 year has passed since last update.

【macOS12.3.1】postgreSQLのインストール、起動、ユーザー作成、テーブル作成備忘録

Posted at

前書き

自分のための備忘録.

  • macOS 12.3.1(intelchip)
  • PostgreSQL 14.2

このサイトを参考に.

インストール

Homebrewを使っているので、以下でインストール

zsh
brew install postgres

無事、インストール完了.スタートの仕方とかも書いてある.

To migrate existing data from a previous major version of PostgreSQL run:
  brew postgresql-upgrade-database

This formula has created a default database cluster with:
  initdb --locale=C -E UTF-8 /usr/local/var/postgres
For more details, read:
  https://www.postgresql.org/docs/14/app-initdb.html

To restart postgresql after an upgrade:
  brew services restart postgresql
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres
==> Summary
🍺  /usr/local/Cellar/postgresql/14.2_1: 3,305 files, 44.3MB
==> Running `brew cleanup postgresql`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Caveats
==> postgresql
To migrate existing data from a previous major version of PostgreSQL run:
  brew postgresql-upgrade-database

This formula has created a default database cluster with:
  initdb --locale=C -E UTF-8 /usr/local/var/postgres
For more details, read:
  https://www.postgresql.org/docs/14/app-initdb.html

To restart postgresql after an upgrade:
  brew services restart postgresql
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/postgresql/bin/postgres -D /usr/local/var/postgres

バージョン確認.

zsh
postgres --version
postgres (PostgreSQL) 14.2

環境設定

PATHを通す

正直これをしなくてもPATHは通ってたっぽいが、一応.(ここ通した方が良い理由教えてほしい)

zsh
echo 'export PATH="/usr/local/opt/postgresql@14/bin:$PATH"' >> ~/.zshrc
$ source .zshrc

デフォルトのDBクラスターのディレクトリを設定する.

localでDBクラスタを作成したときにディレクトリを指定しないとここに作られるらしい.

zsh
$ echo 'export PGDATA="/usr/local/var/postgres"' >> ~/.zshrc

初期化

失敗.空じゃないから一旦リムーブか空にして実行してとのこと.

zsh
$ pg_ctl initdb -o "-E utf8 -U postgres"
The files belonging to this database system will be owned by user "takasukaoru".
This user must also own the server process.

The database cluster will be initialized with locale "ja_JP.UTF-8".
initdb: could not find suitable text search configuration for locale "ja_JP.UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

initdb: error: directory "/usr/local/var/postgres" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgres" or run initdb
with an argument other than "/usr/local/var/postgres".
pg_ctl: database system initialization failed

/usr/local/var/postgres/の中身を全削除して、もう一度実行するとうまくいきました.
ちなみに -E以下はエンコーディング形式.
-U (ユーザー名)でデフォルトユーザーではなく、明記したユーザーをデータベースのスーパーユーザーとして指定できるそう.ひとまず"postgres"にしてみる.

起動

インストールしたときに書かれてた通り、起動してみる.

zsh
$ brew services start postgresql
==> Tapping homebrew/services
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Enumerating objects: 1954, done.
remote: Counting objects: 100% (461/461), done.
remote: Compressing objects: 100% (192/192), done.
remote: Total 1954 (delta 322), reused 332 (delta 263), pack-reused 1493
Receiving objects: 100% (1954/1954), 539.25 KiB | 6.42 MiB/s, done.
Resolving deltas: 100% (877/877), done.
Tapped 1 command (45 files, 689.3KB).
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

起動したっぽい.

データベースの確認.-U (ユーザー名)

zsh
$ psql -l -U postgres
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |
 template0 | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 rows)

ユーザーの確認.postgresしかいない.

zsh
$ psql -c 'select * from pg_user' -U postgres
 usename  | usesysid | usecreatedb | usesuper | userepl | usebypassrls |  passwd  | valuntil | useconfig
----------+----------+-------------+----------+---------+--------------+----------+----------+-----------
 postgres |       10 | t           | t        | t       | t            | ******** |          |
(1 row)

pg_userビューはデータベースユーザに関する情報へのアクセスを提供. これはパスワードフィールドを隠蔽したpg_shadowを公に読めるようにしたビュー、らしい.

ユーザーの作成して、データベース、テーブルを作成

ユーザーの作成

kaoruというユーザーを作ってみる.

zsh
$ createuser -d -e -P kaoru -U postgres
Enter password for new role:
Enter it again:

-d: 新しいユーザーに対して、データベースを作成する権限付与(デフォルトは-D:許可しない)
-e: createuserコマンドが作成して、サーバーに送るコメントをecho表示する.
-P: 新しいユーザーのpasswordを設定する.

データベースの作成

first-dbというDBを作ってみる.

zsh
$ createdb -e -O kaoru first-db "はじめてのPsgreSQLのDB" -U postgres
SELECT pg_catalog.set_config('search_path', '', false);
CREATE DATABASE "first-db" OWNER kaoru;
COMMENT ON DATABASE "first-db" IS 'はじめてのPsgreSQLのDB';

-e: サーバーへ送ってるコメントをecho表示
-O (ユーザー名): 新しいデータベースの所有者となるデータベースユーザを指定

確認してみる.ちゃんとできてる.

zsh
$ psql -l -U postgres
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 first-db  | kaoru    | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |
 postgres  | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |
 template0 | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)

DBに繋いでみる

先ほど作ったDBにユーザーkaoruで繋いでみる.
first-db=> \dで、テーブル一覧を見てみるが、当然何もない.

zsh
$ psql first-db -U kaoru
psql (14.2)
Type "help" for help.

first-db=> \d
Did not find any relations.

テーブル作成

こっからはsqlで色々できる.

sql
first-db=> CREATE TABLE MYTABLE (
first-db(> code char(008),
first-db(> data1 int8,
first-db(> data2 int8,
first-db(> data3 int8
first-db(> );
CREATE TABLE
first-db=> \d
        List of relations
 Schema |  Name   | Type  | Owner
--------+---------+-------+-------
 public | mytable | table | kaoru
(1 row)

とりあえず今回はこれまで.

0
0
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
0
0