Fedora 37 PostgreSQLのテーブルを作成、削除する方法

環境
Fedora release 37 (Thirty Seven)
PostgreSQL 14.3

操作方法
1.テストDBに接続します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ psql testdb
psql (14.3)
"help"でヘルプを表示します。
testdb=#
$ psql testdb psql (14.3) "help"でヘルプを表示します。 testdb=#
$ psql testdb
psql (14.3)
"help"でヘルプを表示します。

testdb=#

2.ユーザーロール一覧を表示します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
testdb=# \du
ロール一覧
ロール名 | 属性 | 所属グループ
----------+------------------------------------------------------------------------------+--------------
arte01 | | {}
postgres | スーパーユーザー, ロール作成可, DB作成可, レプリケーション可, RLS のバイパス | {}
testdb=# \du ロール一覧 ロール名 | 属性 | 所属グループ ----------+------------------------------------------------------------------------------+-------------- arte01 | | {} postgres | スーパーユーザー, ロール作成可, DB作成可, レプリケーション可, RLS のバイパス | {}
testdb=# \du
ロール一覧
ロール名 | 属性 | 所属グループ
----------+------------------------------------------------------------------------------+--------------
arte01 | | {}
postgres | スーパーユーザー, ロール作成可, DB作成可, レプリケーション可, RLS のバイパス | {}

3.データベース一覧を表示します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
testdb=# \l
データベース一覧
名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変換演算子) | アクセス権限
-----------+----------+------------------+-------------+-------------------+-----------------------
postgres | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 |
template0 | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
testdb | arte01 | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 |
(4)
testdb=# \l データベース一覧 名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変換演算子) | アクセス権限 -----------+----------+------------------+-------------+-------------------+----------------------- postgres | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | template0 | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres testdb | arte01 | UTF8 | ja_JP.UTF-8 | ja_JP.UTF-8 | (4 行)
testdb=# \l
                                         データベース一覧
   名前    |  所有者  | エンコーディング |  照合順序   | Ctype(変換演算子) |     アクセス権限
-----------+----------+------------------+-------------+-------------------+-----------------------
 postgres  | postgres | UTF8             | ja_JP.UTF-8 | ja_JP.UTF-8       |
 template0 | postgres | UTF8             | ja_JP.UTF-8 | ja_JP.UTF-8       | =c/postgres          +
           |          |                  |             |                   | postgres=CTc/postgres
 template1 | postgres | UTF8             | ja_JP.UTF-8 | ja_JP.UTF-8       | =c/postgres          +
           |          |                  |             |                   | postgres=CTc/postgres
 testdb    | arte01   | UTF8             | ja_JP.UTF-8 | ja_JP.UTF-8       |
(4 行)

4.テストテーブルを作成します
testdb=# create table user_tbl (userno int, username text);
CREATE TABLE

テーブル一覧を表示します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
testdb=# \dt
リレーション一覧
スキーマ | 名前 | タイプ | 所有者
----------+----------+----------+----------
public | user_tbl | テーブル | postgres
(1)
testdb=# \dt リレーション一覧 スキーマ | 名前 | タイプ | 所有者 ----------+----------+----------+---------- public | user_tbl | テーブル | postgres (1 行)
testdb=# \dt
リレーション一覧
スキーマ | 名前 | タイプ | 所有者
----------+----------+----------+----------
public | user_tbl | テーブル | postgres
(1 行)

テストテーブルにテストデータを挿入します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
testdb=# insert into user_tbl (userno,username) values (101,'toto');
INSERT 0 1
testdb=# insert into user_tbl (userno,username) values (101,'toto'); INSERT 0 1
testdb=# insert into user_tbl (userno,username) values (101,'toto');
INSERT 0 1

データを確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
testdb=# select * from user_tbl;
userno | username
--------+----------
101 | toto
(1)
testdb=# select * from user_tbl; userno | username --------+---------- 101 | toto (1 行)
testdb=# select * from user_tbl;
userno | username
--------+----------
101 | toto
(1 行)

テーブルを削除します。
testdb=> drop table user_tbl;

5.テストデータベースを削除します。
dropdb testdb

Fedora 37

Posted by arkgame