2
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?

More than 1 year has passed since last update.

ORDBMS初心者がM1 MacでPostgresSQLの学習を初める#1

Last updated at Posted at 2021-12-16

はじめに

転職を機に、PostgresSQLの勉強をはじめました。
学習を進める中で、得た内容を整理し今後の自分自身の備忘録として
さらには同じような境遇の方々の為になるような記事を書けるようになりたいと思っています。
間違い等は是非ご指摘ください。

目次

  • 環境
  • PostgresSQLのインストール
  • サンプルデータベース(dvdrental)のセットアップ
  • 参考資料

環境

MacOS Monterey version 12.0.1
psql (PostgreSQL) 14.1

PostgresSQLのインストール

M1 Macの上記バージョンを使用しているが、Apple Silicon Montereyも既にサポート対象に入っている。
(https://formulae.brew.sh/formula/postgresql より)

$ brew install postgresql

サンプルデータベース(dvdrental)のセットアップ

下記リンクに、PostgreSQL tutorialが提供しているdvdrentalというサンプルのデータベースが公開されている。
まずは下記リンクからサンプルのデータベースをダウンロードする。

dvdrental sample db

ダウンロード後、terminalで保存先へ移動し .tarファイルに展開する。

$ cd /your_target_dir
$ unzip dvdrental.zip

PostgresSQLデータベースを起動する。

$ pg_ctl -D /usr/local/var/postgres start

対話型のSQL実行ツールpsqlを実行する。 (データベース名:postgres)

$ psql postgres

上記コマンド実行後は、**postgres=#**のように 
データベース名(postgres)及びsqlプロンプト(=#)が表示される。

新規にdvdrentalという名前のDBを作成する。

postgres=# CREATE DATABASE dvdrental;
#CREATE DATABASE

psqlを抜ける。

postgres=# \q

pg_restoreツールを用いて、ダウンロードしたサンプルDBをdvdrental databaseにロードする。

$ pg_restore -U postgres -d dvdrental.tar
#-U postgres  : PostgreSQLにログインするpostgresuserを指定
#-d dvdrental : ロードするターゲットDBを指定

以上の手順により、Sample DBがダウンロードされている。
psqlツールを使って、データベース:dvdrentalへ接続する。

$ psql dvdrental

下記のように、dvdrentalデータベースへ接続が完了し、対話型プロンプトに入ることができる。

psql (14.1)
Type "help" for help.

dvdrental=#

表を一覧表示する。

dvdrental=# \d

下記のようにDVD Rental ER Model に定義されている表の一覧表示を確認できる。

                           List of databases
                     List of relations
 Schema |            Name            |   Type   |  Owner
--------+----------------------------+----------+----------
 public | actor                      | table    | postgres
 public | actor_actor_id_seq         | sequence | postgres
 public | actor_info                 | view     | postgres
 public | address                    | table    | postgres
 public | address_address_id_seq     | sequence | postgres
 public | category                   | table    | postgres
 public | category_category_id_seq   | sequence | postgres

以上、サンプルデータベースのセットアップとデータベースの接続を行うことができた。
今後は、以下に良さそうなチュートリアルを見つけたので、この内容に沿って学習を進めていく。
https://nuitrcs.github.io/databases_workshop/

参考資料

PostgreSQL Tutorial:
https://www.postgresqltutorial.com/

Homebrew postgresql:
https://formulae.brew.sh/formula/postgresql

Load PostgreSQL Sample Database:
https://www.postgresqltutorial.com/load-postgresql-sample-database/

2
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
2
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?