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?

【データベース】Postgreでのデータベースサーバー構築メモ ~データベース・スキーマ作成~

Last updated at Posted at 2025-05-06

目的

Postgreでのデータベースサーバー構築を行えるようになる

・データベース作成
・スキーマ作成

データベース作成

下記コマンドでデータベース作成

CREATE DATABASE mydb OWNER myuser;

データベース一覧表示(テンプレート含む)

\l

データベース一覧表示(実際のDBのみ)

SELECT datname FROM pg_database WHERE datistemplate = false;
       List of tablespaces
    Name    |  Owner   | Location
------------+----------+----------
 pg_default | postgres |
 pg_global  | postgres |
(2 rows)

PostgreSQL にはデフォルトで template0 と template1 というテンプレートデータベースがある。
pg_database は、PostgreSQLのシステムカタログテーブルで、データベースに関する情報(名前、所有者、作成日など)が格納

データベースの切り替え

\c mydb
You are now connected to database "mydb" as user "postgres".

スキーマ作成

下記コマンドでスキーマ作成

CREATE SCHEMA test_schema AUTHORIZATION myuser;

スキーマ確認

\dn
         List of schemas
    Name     |       Owner
-------------+-------------------
 public      | pg_database_owner
 test_schema | myuser
(2 rows)

スキーマ検索パス設定

セッション中のみ一時的な指定

SET search_path TO スキーマ名1,スキーマ名2,...;

永続的にユーザーの検索パス変更

ALTER ROLE user SET search_path = スキーマ名1,スキーマ名2,...;

スキーマ検索パスの確認

SHOW search_path;

データベースに対してもスキーマ検索パスは設定可能だが、ユーザー設定の方が優先される

ALTER DATABASE DB名 SET search_path TO スキーマ名1,スキーマ名2,...;

ユーザーのスキーマ検索パス設定をリセットする必要があるが実用性は低い

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?