[A-00069]PostgreSQLコマンドまとめ

postgreSQLのコマンドを記載しておく。

下記がかなり参考になるので御一読ください。

https://www.geeksforgeeks.org/postgresql-psql-commands/

・データベースに接続する

psql -d <database-name> -U <user-name> -W
MacBook-Pro:~$ psql -d dwh -U postgres -W
Password: 
psql (15.2)
Type "help" for help.

dwh=#

・データベースを切り替える

\c <database-name>
dwh=# \c postgres
Password: 
You are now connected to database "postgres" as user "postgres".
postgres=# 

・データベース一覧を表示する

\l
postgres-# \l
                                                 List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    | ICU Locale | Locale Provider |   Access privileges   
-----------+----------+----------+-------------+-------------+------------+-----------------+-----------------------
 dwh       | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |            | libc            | 
 postgres  | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |            | libc            | 
 template0 | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
 template1 | postgres | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |            | libc            | =c/postgres          +
           |          |          |             |             |            |                 | postgres=CTc/postgres
(4 rows)

postgres-# 

・テーブル一覧を表示する

\dt
dwh-# \dt
           List of relations
 Schema |   Name    | Type  |  Owner   
--------+-----------+-------+----------
 public | t_manager | table | postgres
 public | t_user    | table | postgres
(2 rows)

・テーブルの列名一覧を表示する

\d <table-name>
dwh-# \d t_manager
                      Table "public.t_manager"
  Column  |          Type          | Collation | Nullable | Default 
----------+------------------------+-----------+----------+---------
 id       | integer                |           | not null | 
 username | character varying(30)  |           | not null | 
 password | character varying(100) |           | not null | 
 delflg   | integer                |           | not null | 

・PostgreSQLのバージョン情報を表示する

任意のデータベースに接続した状態で下記のコマンドを実行することでバージョン情報が確認できます。

select version();
dwh=# select version();
                                                      version                                                      
-------------------------------------------------------------------------------------------------------------------
 PostgreSQL 15.2 on x86_64-apple-darwin20.6.0, compiled by Apple clang version 12.0.0 (clang-1200.0.32.29), 64-bit
(1 row)

・現在のスキーマを確認する

select current_schema;
dwh1=# select current_schema;
 current_schema 
----------------
 dwh_public

・スキーマを切り替える

SET search_path = <schema_name>;
dwh1=# SET search_path = dwh_public;
SET

・スキーマ一覧を表示する

\dn
dwh1=# \dn
        List of schemas
    Name    |       Owner       
------------+-------------------
 dwh_public | dwh_user1
 public     | pg_database_owner
(2 rows)

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

*