[Postgres] - 建立一個 Postgres app 在 fly.io

[Postgres] - Create Postgres app on fly.io

零. < Fly.io Free 方案 >

Free 方案可以免費部署 2 個 App。

- VM size: `shared-cpu-1x`
- Memory: 256 mb

一. < 部署之前要先 Install flyctl >

在操作 fly.io 的過程,都依靠 flyctl 這個 command line tool 來處理,所以這邊要先 Install

以 MacOS 為例:

  • 方法1. Brew:
1
brew install flyctl
  • 方法2. Script:
1
curl -L https://fly.io/install.sh | sh

官網: Install

二. < 接著去官網註冊一個會員 >

在部署前要先 Login

1
flyctl auth login

依照提示很簡單的 Login 進去

登入成功如圖:

三. < Create 一個 Postgres App 在 fly.io 上 >

flyctl postgres create
  • App Name: phil-postgres-test
  • Select region: Japan
  • Select configuration: Development - Single node, 1x shared CPU, 256MB RAM, 1GB disk

接著等待 Deploy 程序完成,會給我們基本 DB connection 的資訊

四. < 取得 DB connection 資訊 >

會給以下這些資訊:

Creating postgres cluster phil-postgres-test in organization personal
Postgres cluster phil-postgres-test created
  Username:    postgres
  Password:    xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  Hostname:    phil-postgres-test.internal
  Proxy Port:  5432
  Postgres Port: 5433
Save your credentials in a secure place -- you won't be able to see them again!


Any app within the xxxxxxxxxx organization can connect to postgres using the above credentials and the hostname "phil-postgres-test.internal."
For example: postgres://postgres:<Password>@phil-postgres-test.internal:5432

Now that you've set up postgres, here's what you need to understand: https://fly.io/docs/reference/postgres-whats-next/

等等連接 DB 時需要用到

五. < 連接至 DB >

Method 1. 直接使用 command line tool flyctl postgres connect 做連接

1
2
3
flyctl postgres connect -a <postgres-app-name>
# ex
flyctl postgres connect -a phil-postgres-test

成功!

Method 2. 使用 flyctl proxy 把 db server port 過渡到 local pc 連接

1
2
3
4
# proxy local port 15432 到遠端的 5432 
flyctl proxy 15432:5432 -a <postgres-app-name>
# ex
flyctl proxy 15432:5432 -a phil-postgres-test

接著

1
psql postgres://postgres:<Password>@localhost:15432

或是

1
2
psql -h localhost -p 15432 -U postgres
# 接著輸入密碼

成功!

六. < 用 psql 查看一下基本 DB 狀態>

  • 看目前 connection info \conninfo
1
2
3
\conninfo

# You are connected to database "postgres" as user "postgres" on host "localhost" (address "127.0.0.1") at port "15432".
  • 看 current user SELECT current_user;
1
2
3
4
5
6
SELECT current_user;

# current_user
# --------------
# postgres
# (1 row)
  • 看目前 active 的 postgres connection
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
SELECT pid as process_id, 
       usename as username, 
       datname as database_name, 
       client_hostname, 
       application_name,
       backend_start,
       state,
       wait_event,
       state_change
FROM pg_stat_activity WHERE state = 'active';

# process_id | username | database_name | client_hostname | application_name |        backend_start         | state  | wait_event |         state_change
# ------------+----------+---------------+-----------------+------------------+------------------------------+--------+------------+-------------------------------
#      17794 | postgres | postgres      |                 | psql             | 2022-10-11 16:16:07.46128+00 | active |            | 2022-10-11 16:42:35.424708+00
(1 row)
  • 列出所有 available 的 databases \l
\l
  • Connect to 指定 DB \c <DB 名> <user 名>
1
2
3
4
5
# 如果 user 名不給,會使用 current user
\c postgres

# psql (14.5, server 14.4 (Debian 14.4-1.pgdg110+1))
# You are now connected to database "postgres" as user "postgres".
  • 列出所有 tables \dt
1
\dt
  • 列出所有 users 和 他們的 roles \du
1
2
3
4
5
6
7
\du

# Role name  |                         Attributes                         | Member of
# ------------+------------------------------------------------------------+-----------
# flypgadmin | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
# postgres   | Superuser                                                  | {}
# repluser   | Replication                                                | {}
  • 查看 command history \s
1
2
3
4
\s

# 將 command history 存進 file
\s <file name>
  • 開啟 query execution time \timing
1
2
3
4
5
6
7
8
\timing

# Timing is on.

# 再下一次 command 就可以關閉
\timing

# Timing is off.
  • Help \?
1
\?
  • Help 並指定特定 command \h <要查詢的相關 command>
1
2
3
4
\h <要查詢的相關 command>

# ex
\h ALTER TABLE
  • 離開 psql \q
1
\q

Ref:

- The End -