err PostgreSQL14 Ubuntu20.04

#ファイルリポジトリ構成を作成します。
sudo sh -c'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main"> /etc/apt/sources.list.d/pgdg.list '

Screenshot from 2021-10-16 12-05-37.png
Screenshot from 2021-10-16 12-06-53.png
リポジトリ署名キーをインポートします。
wget --quiet -O-https://www.postgresql.org/media/keys/ACCC4CF8.asc | 
sudo apt-keyadd-
Screenshot from 2021-10-16 12-09-49.png
Err
sudo: apt-keyadd-: コマンドが見つかりません
#パッケージリストを更新します。
sudo apt-get update

Screenshot from 2021-10-16 12-10-28.png
#最新バージョンのPostgreSQLをインストールします。

#特定のバージョンが必要な場合は、「postgresql」の代わりに「postgresql-12」などを使用してください。

sudo apt-get -y install postgresql
Screenshot from 2021-10-16 12-11-36.png
Screenshot from 2021-10-16 12-11-56.png

sudo -i -u postgres
pg_dump database名 > backup_file名
psql dbname < infile
pg_restore -U "ユーザー名" -C -d "DB名" db.dump

自動起動している?

自動起動している?
101.png
---------------------------------------------------------------------
Your flow credentials file is encrypted using a system-generated key.
If the system-generated key is lost for any reason, your credentials
file will not be recoverable, you will have to delete it and re-enter
your credentials.
You should set your own key using the 'credentialSecret' option in
your settings file. Node-RED will then re-encrypt your credentials
file using your chosen key the next time you deploy a change.
---------------------------------------------------------------------
6 Sep 15:59:31 - [info] Starting flows
6 Sep 15:59:31 - [info] Started flows
6 Sep 15:59:31 - [info] [sqlitedb:d9a2f071.d32e8] opened D:\NodeRed03\test1.db ok
6 Sep 15:59:31 - [info] Server now running at http://127.0.0.1:1880/

PostgREST 8.0.0 + PostgreSQL -13.4-1 + Windows10でテスト(2)

PostgREST 8.0.0 + PostgreSQL -13.4-1 + Windows10でテストをするための、PostgREST 8.0.0のインストールと設定と実行
1. PostgRESTのサイト https://postgrest.org/en/v8.0/releases/v8.0.0.html  から左の v8.0.0 を選択 
001.png
2. PostgREST v8.0.0 release page をクリック github に移動
002.png
3. 下に移動
003.png
4. postgrest-v8.0.0-windows-x64.zip をダウンロードします。
004.png
5. 解凍します
005.png
6. postgrest.exe (t がついています) を 
C:\Program Files\PostgreSQL\13\bin に コピーします。
006.png
7. 貼り付け
007.png
8. 対象のフォルダーへのアクセスは拒否されました
このフォルダーへコピーするには管理者の権限が必要です
と 警告されますが、 
続行
を押します。
008.png
9. postgres に t がつくだけなので、見間違えそうになりますが、別のexe です。
postgres.exe
postgrest.exe 
009.png
cd C:\Program Files\PostgreSQL\13\bin
postgrest 
で動作確認できます、
Usage: postgrest [-e|--example] [--dump-config | --dump-schema] FILENAME
  PostgREST 8.0.0 (0bc7c03) / create a REST API to an existing Postgres database
などが表示されていれば、オッケーです。
010.png
11. Tutorial 0 - Get it Running を選択して、サンプルの todos の動作を実行していきます。
011.png
12. PostgREST 8.0.0 + PostgreSQL -13.4-1 + Windows10でテスト(1)と 10.までの作業で、Tutorial 0 - Get it Runningの
Step 1. Relax, we’ll help  (ステップ1.リラックスしてください)
Step 2. Install PostgreSQL (ステップ2.PostgreSQLをインストールします)
Step 3. Install PostgREST  (ステップ3.PostgRESTをインストールします)
まで、終了しています。
012.png
13. Step 4. Create Database for API(ステップ4.API用のデータベースを作成する)から、今回用にデータベースのパスワードのみを変更して、psqlで実行します。
----------------------
create schema api;
----------------------
create table api.todos (
  id serial primary key,
  done boolean not null default false,
  task text not null,
  due timestamptz
);
----------------------
insert into api.todos (task) values
  ('finish tutorial 0'), ('pat self on back');
----------------------
create role web_anon nologin;
----------------------
grant usage on schema api to web_anon;
----------------------
grant select on api.todos to web_anon;
----------------------
***この行を変更***create role authenticator noinherit login password 'mysecretpassword';
----------------------
create role authenticator noinherit login password 'pg13dbPasswd';
----------------------
grant web_anon to authenticator;
----------------------
\q
----------------------
各行の意味は、https://postgrest.org/en/v8.0/tutorials/tut0.html を参照してください。
013.png
14. psql を実行します
Server [localhost]: Enterですすむ
Database [postgres]: Enterですすむ
Port [5432]: Enterですすむ
Username [postgres]: Enterですすむ
Client Encoding [SJIS]: Enterですすむ
ユーザ postgres のパスワード: pg13dbPasswd (画面には表示されません) を入力して、Enterですすむ
psql (13.4)
"help"でヘルプを表示します。
postgres=# が表示されれば、オッケー
014.png
15. 続行するには何かキーを押してください . . .
で Enterを押すと、画面がクローズされます。
015.png
16. Step 5. Run PostgREST (ステップ5.PostgRESTを実行します)
***この行を変更***db-uri = "postgres://authenticator:mysecretpassword@localhost:5433/postgres"
db-uri = "postgres://authenticator:pg13dbPasswd@localhost:5432/postgres"
db-schema = "api"
db-anon-role = "web_anon"
で、tutorial.conf を作成します
016.png
17. メモ帳を管理者として実行します。
017.png
18. 
018.png
19. C:\Program Files\PostgreSQL\13\bin
tutorial.conf
として、保存します。
019.png
20. × で閉じます。
020.png
21. サイトの ./postgrest tutorial.conf は 実行するのがWindowsなので
cd C:\Program Files\PostgreSQL\13\bin
postgrest tutorial.conf
となります。
このアプリの機能のいくつかがwindows defenderファイアウォールでブロックされています
の警告がでるので、
アクセスを許可する
を押します。
021.png
22. 下記のように表示で、起動しています。
C:\Program Files\PostgreSQL\13\bin>postgrest tutorial.conf
25/Aug/2021:18:32:00 +0900: Attempting to connect to the database...
25/Aug/2021:18:32:00 +0900: Connection successful
25/Aug/2021:18:32:00 +0900: Listening on port 3000
25/Aug/2021:18:32:00 +0900: Config re-loaded
25/Aug/2021:18:32:00 +0900: Listening for notifications on the pgrst channel
25/Aug/2021:18:32:00 +0900: Schema cache loaded
022.png
23. サイトの curl http://localhost:3000/todos
は、ブラウザで
http://localhost:3000/todos と入力して、確認します。
正しく、jsonが返されています。
[{"id":1,"done":false,"task":"finish tutorial 0","due":null}, 
 {"id":2,"done":false,"task":"pat self on back","due":null}]
023.png

実行時のエラー
portが違うと
err_001_R8_023_pg13_Step5_5433err.png
tutorial.conf の文字コードが違うと
err_002.png
のような、エラーとなりました。

サイトのインストール方法のscoopなとで、

PostgREST 8.0.0 + PostgreSQL -13.4-1 + Windows10でテスト(1)

PostgREST 8.0.0 + PostgreSQL -13.4-1 + Windows10でテストをするための、PostgreSQL -13.4-1のインストール
1. https://www.postgresql.org/download/windows/ から Download the installer をクリック。
001.png
Windows x86-64 13.4 をダウンロードします。
002.png
3.
003.png
4. postgresql-13.4-1-windows-x64.exe を実行。
004.png
5.
005.png
6.
006.png
7.
007.png
8.
008.png
9. Stack Builder は使用しないので、チェックを外します。
(pgAdmin4 はインストールしなくても、この作業は、可能です、また最新版をインストールしてもオッケーです)
009.png
10.
010.png
11.  後述の、PostgREST 8.0.0でも使用する、PostgreSQLのパスワードです。
pg13dbPasswd
pg13dbPasswd
とします(大文字小文字に注意)
011.png
12. 標準の 5432 ポートとします
後述の、PostgREST 8.0.0の Tutorial 0 - Get it Running (https://postgrest.org/en/v8.0/tutorials/tut0.html) では 5433 ですが、標準の 5432 で進めます。
012.png
13. ロケールを選択します。
013.png
14. 今回は、 C とします。
(たぶんDefalut またはJapaneseでも問題ないと思います)
014.png
15.
015.png
16.
016.png
17. インストール中です。
017.png
18.
018.png
19. インストールされた、SQL Shell (psql) を使います。
019.png
20. 必要があれば、(今回の作業では、pg_hba.conf / postgresql.conf の編集はなしでも動作確認できます)
C:\Program Files\PostgreSQL\13\data の pg_hba.conf
postgresql.conf を編集します。
(postgresql.confは、https://pgtune.leopard.in.ua/#/ のサイトを使用すると大変便利です)
020.png
21.
021.png
22. pg_hba.confを編集
今回は、ローカルの他の機器から接続確認もできるように、追記します。
(192.168.20.0/24         scram-sha-256を追加すると、ローカルエリアの 192.168.20.xxx アドレスから scram-sha-256 で接続が可能となります。)
host    all             all             127.0.0.1/32           scram-sha-256 
host    all             all             192.168.20.0/24      scram-sha-256
022.png
23. pg_hba.confを編集後、PostgreSQLの再起動が必要です。
スタートから右クリックで、コンピューターの管理
023.png
24. サービスとアプリケーションから
サービスのpostgres-x64-13 を選択
024.png
25. 停止
025.png
26. 開始
(この手順が面倒な方は、Windowsの再起動でもオッケーです)
026.png
27. (Windowsにpostgresをインストールした場合、デフォルトで
listen_addresses = '*'
port = 5432
がのようにコメントアウトされた状態になっています)
027.png
28. pgAdmin4 で接続の確認をしてみます。
028.png