WSL2-Ubuntu22.04でPostgresql環境構築

# Postgresql 環境構築
```sh
sudo apt update
sudo apt install postgresql postgresql-contrib
```
## バージョン確認
```sh
psql --version
```
```
>>> psql (PostgreSQL) 14.8 (Ubuntu 14.8-0ubuntu0.22.04.1)
```
# Postgresql使い方
## 起動
```sh
sudo service postgresql start
```
## ステータス確認
```sh
sudo service postgresql status
```

```sh
>>> * Starting PostgreSQL 14 database server   [ OK ]
```
## postgresでログイン
```
sudo -u postgres -i 
```
```sh
postgres@DESKTOP-29D8DSQ:~$ psql
psql (14.8 (Ubuntu 14.8-0ubuntu0.22.04.1))
Type "help" for help.

postgres=#
```
## データベース一覧
```sql
postgres=# \l
                              List of databases
   Name    |  Owner   | Encoding | Collate |  Ctype  |   Access privileges
-----------+----------+----------+---------+---------+-----------------------
 postgres  | postgres | UTF8     | C.UTF-8 | C.UTF-8 |
 template0 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
 template1 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
```

## Role一覧
```sql
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
```

## SuperUserでRole作成
```sql
postgres=# CREATE ROLE <new_name> WITH SUPERUSER LOGIN PASSWORD '<pass>';
CREATE ROLE
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 new_name  | Superuser                                                  | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
```

## 新規RoleがOwnerのDataBase作成
```sql
postgres=# CREATE DATABASE <db_name> WITH OWNER <ubuntu_name>;
CREATE DATABASE
postgres-# \l
                                 List of databases
    Name    |     Owner     | Encoding | Collate |  Ctype  |   Access privileges
------------+---------------+----------+---------+---------+-----------------------
 <db_name>  | <ubuntu_name> | UTF8     | C.UTF-8 | C.UTF-8 |
 postgres   | postgres      | UTF8     | C.UTF-8 | C.UTF-8 |
 template0  | postgres      | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
            |               |          |         |         | postgres=CTc/postgres
 template1  | postgres      | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
            |               |          |         |         | postgres=CTc/postgres
```

## Roleを指定してDataBaseに接続
```sh
psql <ubuntu_name> -d <db_name>
psql (14.8 (Ubuntu 14.8-0ubuntu0.22.04.1))
Type "help" for help.

analysis=#
```

## 停止
```sh
sudo service postgresql stop
```
```sh
>>> * Stopping PostgreSQL 14 database server     [ OK ]
```

# 参考にしたサイト
https://rowingfan.hatenablog.jp/entry/2020/11/07/135110

https://www.javadrive.jp/postgresql/role/index2.html#section1

https://qiita.com/domodomodomo/items/04026157b75324e4ea27