こんにちは、やすです。
今回は、Linux(CentOS7)のサーバにて、PostgreSQLでデータベースを作成・削除する方法をご紹介します。
PostgreSQLの場合は、インストールするとデフォルトで「postgres」「template0」「template1」のデータベースが作成されます。
自分用の学習ならそのまま「postgres」のデータベースを使ってもいいんですが、通常は新しくデータベースを作成します。
データベースの作成・削除
事前確認
(1)postgresユーザであることを確認
[root@localhost ~]# su - postgres
最終ログイン: 2024/01/10 (水) 13:32:16 JST日時 pts/0
-bash-4.2$
-bash-4.2$ whoami
postgres
-bash-4.2$
(2)データベースの一覧を確認(作成対象のデータベースがないこと)
-bash-4.2$ psql -l
データベース一覧
名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変換演算子) | アクセス権
-----------+----------+------------------+----------+-------------------+-----------------------
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
(3 行)
-bash-4.2$
(3)とりあえずpostgresデータベースに接続
-bash-4.2$ psql postgres
psql (9.2.24)
"help" でヘルプを表示します.
postgres=#
データベースの作成
(4)データベース作成
postgres=# CREATE DATABASE test1;
CREATE DATABASE
postgres=#
データベースの削除
削除は作成の逆で、削除したいデータベースがあることを確認して下記を実行します。
(4)データベース削除
postgres=# DROP DATABASE test1;
DROP DATABASE
postgres=#
事後確認
(5)データベース切断
postgres=# \q
-bash-4.2$
(6)データベースの一覧を確認
-bash-4.2$ psql -l
データベース一覧
名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変換演算子) | アクセス権
-----------+----------+------------------+----------+-------------------+-----------------------
postgres | postgres | UTF8 | C | C |
template0 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C | C | =c/postgres +
| | | | | postgres=CTc/postgres
test1 | postgres | UTF8 | C | C |
(4 行)
-bash-4.2$
まとめ
今回は、データベースの作成・削除のやり方を紹介しました。
ではまた、バイバーイ♪