SQLの練習PostgreSQL
備忘として、データセットの作成やPostgreSQLのコマンドをメモしておきます。
データセット作成
POSTGRESQL TUTORIALのサンプルデータセットを使用。
https://www.postgresqltutorial.com/postgresql-getting-started/postgresql-sample-database/
対話形式のシェルに移動
work用のPostgreSQLサーバを起動し、psqlで接続します。
dockerで用意しているので、コンテナ起動からです。
コンテナ起動
$ docker compose up -d
# -d はバックグラウンド実行
postgres接続
$ psql -h localhost -p 5432 -U postgres
# -h DBサーバのホスト指定
# -p DBサーバのポート番号指定
# -U DBサーバの接続ユーザ名指定
データセット用のデータベース作成
CREATE DATABASE dvdrental;
tarファイルを使った展開
pg_restore -h localhost -p 5432 -U postgres -d dvdrental ./dvdrental.tar
Databaseへ接続
psql -h localhost -p 5432 -d dvdrental -U postgres