LoginSignup
10
3

More than 1 year has passed since last update.

PostgreSQLのインストールと基本的な使い方-Mac編-

Last updated at Posted at 2023-02-03

記事の概要

PostgreSQLをMacにインストールして、基本的な使い方をまとめてみた。

本編

Homebrewの確認

brew --version
# Homebrew 3.6.17
# Homebrew/homebrew-core (git revision 04d0476d1de; last commit 2023-01-02)
# Homebrew/homebrew-cask (git revision 9a37837650; last commit 2023-01-02)

インストールできるpostgreSQLを確認

brew search postgresql
#==> Formulae
#postgresql@10       postgresql@12       postgresql@14 ✔     postgresql@9.4      qt-postgresql
#postgresql@11       postgresql@13       postgresql@15       postgresql@9.5      postgrest

#==> Casks
#navicat-for-postgresql
#If you meant "postgresql" specifically:
#postgresql breaks existing databases on upgrade without human intervention.

#See a more specific version to install with:
#  brew formulae | grep postgresql@

チェックがついたpostgresql@14がデフォルトでインストールされる。バージョンを指定したい場合は、postgresql@バージョンで以下を実行する。

PostgreSQlをインストール

brew install postgresql
#Running `brew update --auto-update`...
#==> Auto-updated Homebrew!
#Updated 3 taps (homebrew/core, homebrew/cask and homebrew/services).
#==> New Formulae
#・・・

バージョンの確認実施。

psql --version
#psql (PostgreSQL) 14.6 (Homebrew)

PostgreSQLを起動する

brew services start postgresql
#Warning: Use postgresql@14 instead of deprecated postgresql
#==> Successfully started `postgresql@14` (label: #homebrew.mxcl.postgresql@14)

PostgreSQLに接続する

PostgreSQLに接続する際は、
「psql -h ホスト名/IPアドレス -p ポート番号 -U ロール名 -d データベース名」を用いる。
ホスト名/IPアドレスは、ローカルホストの場合は、localhost
ポード番号のデフォルトは、5432
ロール名のデフォルトは、OSのユーザー名(スーパーユーザー)
データベースのデフォルトは、postgres

psql -h localhost -p 5432 -U OSのユーザー名 -d postgres
#psql (14.6 (Homebrew))
#Type "help" for help.

#postgres=# 

よく使うコマンド

ロールの作成

PostgreSQLに接続する前でも後でもロールを作成できる。権限の付与は今回は省略する。
接続前の場合
ロールをパスワードと共に作成するには、「createuser -P ロール名」である。

createuser -P niwa
#Enter password for new role: 
#Enter it again: 

接続後の場合

create role teru with login password 'aki';
#CREATE ROLE

ロール作成と同様に、PostgreSQlへ接続前、つまりShellからさまざまなものを実行することができるが、本記事ではPostgreSQLに接続後のコマンドに限定する。

データベースの作成

CREATE DATABASE db;
#CREATE DATABASE

テーブルの作成

作成したデータベース(db)下にテーブルを作成する。

psql db

#psql (14.6 (Homebrew))
#Type "help" for help.

CREATE TABLE "営業拠点マスター"
("営業所番号" text not null,
"営業拠点" text not null,
"郵便番号" text not null,
"住所" text not null,
PRIMARY KEY("営業所番号"));
#CREATE TABLE

テーブルの挿入

INSERT INTO "営業拠点マスター"("営業所番号","営業拠点","郵便番号","住所") VALUES ('JP_BO_1','札幌支店','065-0033','札幌市東区北33条東5丁目1番28号');
#INSERT 0 1

テーブルのデータ取得

select * from "営業拠点マスター";
# 営業所番号 | 営業拠点 | 郵便番号 |              住所              
#------------+----------+----------+--------------------------------
# JP_BO_1    | 札幌支店 | 065-0033 | 札幌市東区北33条東5丁目1番28号
#(1 row)

データベースの一覧表示

\l
#                             List of databases
#   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   
#-----------+----------+----------+---------+-------+-----------------------
# db        | jp420521        | UTF8     | C       | C     | 
# postgres  | OSのユーザー名 | UTF8     | C       | C     | 
# template0 | OSのユーザー名 | UTF8     | C       | C     | =c/OSのユーザー名          +
#           |                  |          |         |       | OSのユーザー名=CTc/OSのユーザー名
# template1 | OSのユーザー名 | UTF8     | C       | C     | =c/OSのユーザー名          +
#           |                 |          |         |       | OSのユーザー名=CTc/OSのユーザー名

ロールの一覧表示

\du
#                                   List of roles
# Role name |                         Attributes                         | Member of 
#-----------+------------------------------------------------------------+-----------
# OSのユーザー名  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
# niwa      |                                                            | {}
# teru      |                                                            | {}

スキーマの一覧表示

\dn
#  List of schemas
#  Name  |  Owner   
#--------+----------
# public | jp420521
#(1 row)

テーブルの一覧表示

\dt
#              List of relations
# Schema |       Name       | Type  |  Owner   
#--------+------------------+-------+----------
# public | 営業拠点マスター | table | jp420521
#(1 row)

##テーブルの詳細表示
\d '営業拠点マスター'
#          Table "public.営業拠点マスター"
#   Column   | Type | Collation | Nullable | Default 
#------------+------+-----------+----------+---------
# 営業所番号 | text |           | not null | 
# 営業拠点   | text |           | not null | 
# 郵便番号   | text |           | not null | 
# 住所       | text |           | not null | 
#Indexes:
#    "営業拠点マスター_pkey" PRIMARY KEY, btree ("営業所番号")
10
3
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
10
3