0
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(M1)でPostgreSQLの環境構築してみた

Posted at

目的

PostgreSQL環境をMac(M1)上に構築する。
以下の順で構築を進める。
①HomebrewでPosgreSQLをインストールする
②PosgreSQLを起動する
③DBに接続してテーブルを作成する

注意点(既にHomebrewでパッケージ管理されている場合)

x86_64ベースのHomebrewを使っている場合、この手順は実施しないでください。
(パッケージ管理が面倒になります。)

ターミナルで以下のコマンド入力し、環境を確認してください。

% uname -u
arm64
% which brew
/opt/homebrew/bin/brew
-->こんな感じで表示されたらOK
% uname -u
x86_64
% which brew
/usr/local/bin/brew
-->こんな感じで表示されたらNG

事前作業

Homebrewを使うので、インストールする。
インストールされていない場合は以下のサイトを参考にインストールすること。
macOS(またはLinux)用パッケージマネージャー — Homebrew

インストール後、以下のコマンドでARMベース版がインストールされていることを確認すること。

% which brew
/opt/homebrew/bin/brew

①HomebrewでPostgreSQLをインストールする

ターミナル上で以下のコマンドを入力し、インストールを実行する

% brew install postgresql@14

成功すると以下のように表示されてインストールが完了する

To start postgresql@14 now and restart at login:
  brew services start postgresql@14
Or, if you don't want/need a background service you can just run:
  /opt/homebrew/opt/postgresql@14/bin/postgres -D /opt/homebrew/var/postgresql@14

インストールが完了したら、以下のコマンドでPostgreSQLのバージョンが表示されていることを確認しておきます。

% psql -V
psql (PostgreSQL) 14.15 (Homebrew)

②PosgreSQLを起動する

以下のコマンドを入力し、PostgreSQLを起動する

% postgres -D /opt/homebrew/var/postgresql@14
LOG:  starting PostgreSQL 14.15 (Homebrew) on aarch64-apple-darwin24.1.0, compiled by Apple clang version 16.0.0 (clang-1600.0.26.4), 64-bit
LOG:  listening on IPv6 address "::1", port 5432
LOG:  listening on IPv4 address "127.0.0.1", port 5432
LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
LOG:  database system was shut down at YYYY-MM-DD HH:MM:SS
LOG:  database system is ready to accept connections

➡︎こんな感じでログが表示されれば起動完了!

停止するときは[control]+[C]で終了できます。

LOG:  received fast shutdown request
LOG:  aborting any active transactions
LOG:  background worker "logical replication launcher" (PID XXXXX) exited with exit code 1
LOG:  shutting down
LOG:  database system is shut down

③DBに接続してテーブルを作成する

DBを起動している状態で、新規ウインドでターミナルを起動し、以下のコマンドで起動したDBに接続をする

% psql -d postgres
psql (14.15 (Homebrew))
Type "help" for help.

postgres=# 

「postgres=#」のようにPostgreSQLのプロンプトに変われば接続成功です。


試しにテーブルを作成してみる

postgres=# CREATE TABLE table1 (
    id SERIAL PRIMARY KEY,
    name VARCHAR(50),
    category VARCHAR(20),
    price DECIMAL(10,2),
    stock INT,
    created_at TIMESTAMP,
    updated_at TIMESTAMP,
    description TEXT,
    status VARCHAR(10),
    supplier_id INT
);
CREATE TABLE
postgres=# 

テーブルが問題なく作成できましたね!

終わりに

今回、M1チップのMac環境でPostgreSQLの構築をしてみました。
問題なく構築できましたでしょうか??
MacだとHomebrewに惑わされることが多々あるので、自分が今どのような環境でHomebrewを使っているのかを確認してから作業するのをオススメします!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?