Skip to content

Instantly share code, notes, and snippets.

@hatappo
Last active January 29, 2026 02:08
Show Gist options
  • Select an option

  • Save hatappo/d0c38814851c3bd5993fccee465fd586 to your computer and use it in GitHub Desktop.

Select an option

Save hatappo/d0c38814851c3bd5993fccee465fd586 to your computer and use it in GitHub Desktop.
Listing SQLs of PostgreSQL. note
[ PostgreSQL Instance ] (サーバー / クラスター)
       
       ├── [ Database A ] (独立したデータ空間)
             
             ├── < Schema: public > (デフォルト)
                   ├── Table: users
                   └── Table: orders
             
             └── < Schema: inventory > (論理的なグループ)├── Table: products└── Table: stocks
       
       └── [ Database B ]
              └── < Schema: public >
                     └── Table: logs

1.データベース一覧

/*List Databases*/
\l+
/*List Databases*/
SELECT 
    datname AS database_name,
    pg_get_userbyid(datdba) AS owner,
    pg_size_pretty(pg_database_size(datname)) AS size
FROM pg_database
WHERE datistemplate = false;

2.スキーマ一覧

/*List Schemas*/
\dn+
/*List Schemas*/
SELECT schema_name 
FROM information_schema.schemata
WHERE schema_name NOT IN ('information_schema', 'pg_catalog') 
  AND schema_name NOT LIKE 'pg_toast%'
  AND schema_name NOT LIKE 'pg_temp%';

3.テーブル一覧

/*List Tables*/
\d
\ds     -- sequence のみ
\dt+    -- table のみ
/*List Tables*/
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'
  AND table_schema NOT IN ('information_schema', 'pg_catalog')
ORDER BY table_schema, table_name;
/*Change default search schema*/
SHOW search_path;
SET search_path=public;

4.カラム一覧

/*List Columns*/
\d TABLE_NAME
/*List Columns*/
SELECT column_name, data_type, is_nullable, column_default
FROM information_schema.columns
WHERE table_name = 'TABLE_NAME'/*変更*/
ORDER BY ordinal_position;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment