1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MacでpostgreSQLの環境構築 〜 インストールからテーブル作成まで実行する 〜

Last updated at Posted at 2024-02-10

MacでpostgreSQLの環境構築に思ったより時間がかかった。
二度と困ることがないようにメモしておく。
「postgreSQLのインストール」 → 「初回起動」 → 「テーブル作成」 という流れで記載している。
なお、自分のMacはM1のアップルシリコン製のMacです。

1.postgreSQLのインストール

ターミナルからコマンド入力でインストールする。
インストールできるバージョンは以下のコマンドで確認可能

$ brew search postgresql

インストールをする時はバージョンの指定をしたほうが良いとのこと。
以下のコマンドで安定していると評判のv14をインストールする。
なお、以下はHomeBrewを使用したインストールコマンドである。

$ brew install postgresql@14

インストールにはけっこう時間がかかった。自分はプログレスバーが100%に到達してからが長かった。
finishの文字が出てから、ターミナルが入力待機状態に戻るまで4分くらいはかかった。

インストールが完了したら一度以下のコマンドでインストールされたバージョンを確認しておくのが吉。

$ psql --version
psql (PostgreSQL) 14.11 (Homebrew)

上のようにバージョン情報が表示されていればOK。

2.postgreSQLの起動確認

インストールしたpostgreSQLのサーバーを立ち上げ、動作確認をしてみる。
以下のコマンドでposgreSQLをたち上げることが可能。

postgres -D /usr/local/var/postgres

コマンド実行後、「accept connections」と表示されば正常に起動が完了している。

tspc:~ ts$ postgres -D /usr/local/var/postgres
2024-02-10 20:40:23.962 JST [33193] LOG:  starting PostgreSQL 14.11 (Homebrew) on x86_64-apple-darwin22.6.0, compiled by Apple clang version 15.0.0 (clang-1500.1.0.2.5), 64-bit
2024-02-10 20:40:23.963 JST [33193] LOG:  listening on IPv6 address "::1", port 5432
2024-02-10 20:40:23.963 JST [33193] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2024-02-10 20:40:23.963 JST [33193] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2024-02-10 20:40:23.973 JST [33201] LOG:  database system was shut down at 2024-02-10 17:55:55 JST
2024-02-10 20:40:23.998 JST [33193] LOG:  database system is ready to accept connections

立ち上がった事を確認したら、以下のコマンドでデータベースの一覧を確認してみる。

tspc:~ ts$ psql -l
                              List of databases
   Name    | Owner | Encoding |   Collate   |    Ctype    | Access privileges
-----------+-------+----------+-------------+-------------+-------------------
 postgres  | ts    | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |

上記のような感じで初期設定のDBが1つは表示されるはず。

なお、サーバーの起動をストップするには 「ctrl + c」を入力する。
以下のようなログが表示された後にストップする。

2024-02-10 20:46:58.721 JST [33193] LOG:  received fast shutdown request
2024-02-10 20:46:58.721 JST [33193] LOG:  aborting any active transactions
2024-02-10 20:46:58.727 JST [33193] LOG:  background worker "logical replication launcher" (PID 33209) exited with exit code 1
2024-02-10 20:46:58.728 JST [33204] LOG:  shutting down
2024-02-10 20:46:58.735 JST [33193] LOG:  database system is shut down

なお、brewによる起動コマンドもあるが、過去に一度別のバージョンのpostgreをインストールしていたせいか、brewによるサーバー起動コマンドを実行したらエラー状態になった。
この状態からリカバリーするため、なんだかんだで全てのバージョンのpostgreをアンインストール→v14をインストールし直すという状況に陥り、かなりの時間を消耗してしまった...。

3.postgreSQLに接続する

postgreSQLサーバーを立ち上げた状態で別ウィンドウのターミナルを起動し、以下のコマンドを実行してposgreSQLに接続する。

$ psql -d postgres

コマンドを実行すると以下のように表記が変わり、postgreSQLコマンドの入力待ち状態になる。

postgres=#

4.テーブル新規作成

テーブルを作成するため、まずは以下のコマンドでデータベースに接続する。
最初から用意されていた、[postgres]というデータベースに接続してみる。

-- \c [接続するデータベース名] 
postgres=# \c postgres

DBへの接続が完了したら以下のcreate文でテーブルを作成する

CREATE TABLE test_table
(
 id SERIAL PRIMARY KEY,
 name VARCHAR(255),
 age INT
);

-- コマンド実行結果
CREATE TABLE

作成したテーブルを確認するコマンド

postgres=# \dt

          List of relations
 Schema |    Name    | Type  | Owner
--------+------------+-------+-------
 public | test_table | table | ts
(1 row)

作成したテーブルのカラム情報を確認するコマンド

-- \d+ [テーブル名]
postgres=# \d+ test_table

一連のコマンドはだいたいこんな感じです。ちなみに、終了する時はexitを入力すればOK

postgres=# exit
1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?