PostgreSQL 14.2にデータベースとテーブルを作成する方法

環境
OSバージョンの確認
# cat /etc/redhat-release
Rocky Linux release 8.5 (Green Obsidian)
PostgreSQL 14.2

1.postgres ユーザに切り替え

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# su - postgres
[postgres@localhost ~]$ psql
psql (14.2)
"help"でヘルプを表示します。
postgres=#
# su - postgres [postgres@localhost ~]$ psql psql (14.2) "help"でヘルプを表示します。 postgres=#
# su - postgres
[postgres@localhost ~]$ psql
psql (14.2)
"help"でヘルプを表示します。

postgres=#

2.データベースの一覧を確認します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
postgres=# \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
(3)
postgres=# \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 (3 行)
postgres=# \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
(3 行)

3.データベースを作成します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
postgres=# create database testdb;
CREATE DATABASE
postgres=# create database testdb; CREATE DATABASE
postgres=# create database testdb;
CREATE DATABASE

4.作成したデータベースを操作する場合は一度、psql を終了させるため次のコマンドを実行します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
postgres=# \q
[postgres@localhost ~]$
postgres=# \q [postgres@localhost ~]$
postgres=# \q
[postgres@localhost ~]$

5.作成したデータベースに接続します

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

testdb=#

6.PostgreSQL の DB にテーブルを作成します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
testdb=# CREATE TABLE usertbl
(
uid serial NOT NULL,
uname text NOT NULL,
PRIMARY KEY (uid)
);
CREATE TABLE
testdb=# CREATE TABLE usertbl ( uid serial NOT NULL, uname text NOT NULL, PRIMARY KEY (uid) ); CREATE TABLE
testdb=# CREATE TABLE usertbl
(
      uid serial NOT NULL,
      uname text NOT NULL,
      PRIMARY KEY (uid)
);
CREATE TABLE

7.テーブルの一覧を確認します

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

 

PostgreSQL

Posted by arkgame