PostgreSQL User၊ Role နဲ့ Ownership
PostgreSQL 19.0
Port 5432 နဲ့ run နေတဲ့ local PostgreSQL server မှာ လိုက်စမ်းပါ။ Administrator role postgres နဲ့ စပြီး လေ့ကျင့်ဖို့ mydb database အသစ်နဲ့ nyinyizin login role အသစ်ကို သုံးမယ်။ အပိုင်းတွေကို အစဉ်လိုက် လုပ်ပါ။ psql ဖွင့်တဲ့ command ကို OS terminal မှာ ရိုက်ပြီး SQL နဲ့ backslash commands ကို psql ထဲမှာ ရိုက်ပါ။
Code စမ်းလို့ရတဲ့ သင်ခန်းစာ ဖွင့်ရန်
ဒီစာမျက်နှာမှာ
- postgres နဲ့ psql ဝင်ခြင်း
- Login Role ဆောက်ပြီး Password သတ်မှတ်ခြင်း
- Role Attribute တစ်ခုချင်း ပေးခြင်းနဲ့ ပြန်ဖြုတ်ခြင်း
- SUPERUSER Attribute ကို နားလည်ပြီး ပြန်ဖြုတ်ခြင်း
- mydb ဆောက်ပြီး Owner သတ်မှတ်ခြင်း
- nyinyizin နဲ့ ပြန်ဝင်ခြင်း
- Role အသစ်ပိုင်တဲ့ Table တစ်ခု ဆောက်ခြင်း
- mydb ကို postgres နဲ့ ပြန်ဝင်ခြင်း
- Owner ပြန်ပြောင်းပြီး Login Role ဖျက်ခြင်း
- တရားဝင် ကိုးကားချက်များ
postgres နဲ့ psql ဝင်ခြင်း
ဒီ command ကို terminal မှာ ရိုက်ပါ။ Setup မှာ သုံးခဲ့တဲ့ postgres role နဲ့ ဝင်ပြီး password တောင်းရင် ထည့်ပါ။ ကိုယ့်စက်မှာ administrator role နာမည် ကွာရင် နောက်ဆုံး owner ပြန်ပြောင်းတဲ့ အဆင့်အပါအဝင် အဲဒီ role ကို တစ်လျှောက်လုံး သုံးပါ။ mydb သို့မဟုတ် nyinyizin ရှိပြီးသားဆိုရင် နမူနာတစ်လျှောက်လုံး နာမည်အသစ်တွေ သုံးပါ။
psql -h localhost -p 5432 -U postgres -d postgres
Login Role ဆောက်ပြီး Password သတ်မှတ်ခြင်း
ဒီ command တွေကို psql ထဲမှာ ရိုက်ပါ။ CREATE USER က LOGIN ပါတဲ့ role ဆောက်ပေးတယ်။ Password ကို နှစ်ခါရိုက်ရပြီး ရိုက်နေတဲ့ စာလုံးတွေ မပေါ်ပါဘူး။ နောက်ဆုံးတစ်ကြောင်းနဲ့ role အသစ် ရှိမရှိ စစ်ပါ။
CREATE USER nyinyizin;
\password nyinyizin
\du nyinyizin
CREATE USER … WITH PASSWORD- CREATE USER မှာ WITH PASSWORD နဲ့လည်း password သတ်မှတ်လို့ရတယ်။ Terminal လေ့ကျင့်ခန်းမှာ password ကို ရိုက်တဲ့ SQL ထဲ မပါစေဖို့ \password ကို သုံးပါ။
Role Attribute တစ်ခုချင်း ပေးခြင်းနဲ့ ပြန်ဖြုတ်ခြင်း
ဒီအပိုင်းကို စမ်းချင်ရင် database ဆောက်ခွင့်အတွက် CREATEDB၊ သက်ဆိုင်တဲ့ role တွေ ဆောက်ဖို့နဲ့ စီမံဖို့ CREATEROLE ပေးကြည့်ပါ။ \du နဲ့ ရလဒ်စစ်ပြီး နောက်အဆင့်မသွားခင် နှစ်ခုလုံး ပြန်ဖြုတ်ပါ။ ဒီ attributes တွေ ပြောင်းတာက role ရဲ့ LOGIN setting ကို မပြောင်းပါဘူး။
ALTER USER nyinyizin WITH CREATEDB CREATEROLE;
\du nyinyizin
ALTER USER nyinyizin WITH NOCREATEDB NOCREATEROLE;
SUPERUSER Attribute ကို နားလည်ပြီး ပြန်ဖြုတ်ခြင်း
Superuser က login လုပ်ခွင့် စစ်တာကလွဲပြီး PostgreSQL permission စစ်ဆေးမှုတွေကို ကျော်နိုင်တယ်။ ဖျက်ပစ်လို့ရတဲ့ local စမ်းသပ် server မှာ ဒီဥပမာကို စမ်းနိုင်တယ်။ postgres နဲ့ transaction တစ်ခုထဲမှာ attribute ပေး၊ စစ်ပြီး commit မလုပ်ခင် အဆင့်မြင့် attributes တွေကို ပြန်ဖြုတ်ပါ။ ယာယီပေးထားတဲ့ superuser ခွင့်က အဲဒီ transaction ထဲမှာပဲ သက်ရောက်တယ်။
BEGIN;
ALTER USER nyinyizin WITH SUPERUSER;
\du nyinyizin
ALTER USER nyinyizin WITH NOSUPERUSER NOCREATEROLE NOCREATEDB NOREPLICATION NOBYPASSRLS;
COMMIT;
\du nyinyizin
mydb ဆောက်ပြီး Owner သတ်မှတ်ခြင်း
postgres နဲ့ ချိတ်ထားတုန်း practice database ဆောက်ပြီး owner ကို nyinyizin ဆီ လွှဲပါ။ အဆင့်မြင့် attributes တွေ ဖြုတ်ထားလည်း ဒီ database ကို nyinyizin ပိုင်လို့ရတယ်။ Owner column မှာ nyinyizin ပေါ်လာတာ စစ်ပြီး \q နဲ့ psql ကနေ ထွက်ပါ။
CREATE DATABASE mydb;
ALTER DATABASE mydb OWNER TO nyinyizin;
\l mydb
\q
ALTER DATABASE … OWNER TO- Database ကိုယ်တိုင်ရဲ့ owner ကို ပြောင်းပေးတယ်။ Database ထဲက table တွေမှာတော့ သူတို့ကိုယ်ပိုင် owner ရှိတယ်။
nyinyizin နဲ့ ပြန်ဝင်ခြင်း
OS terminal မှာ ဒီ command ရိုက်ပြီး nyinyizin အတွက် သတ်မှတ်ထားတဲ့ password ကို ထည့်ပါ။ အဆင့်မြင့် attributes မရှိတဲ့ ဒီ role နဲ့ ဝင်ရင် default prompt က mydb=> ပါ။
psql -h localhost -p 5432 -U nyinyizin -d mydb
psql- PostgreSQL command-line client ကို ဖွင့်တယ်။
-h localhost- ကိုယ့်စက်ပေါ်က server ကို TCP နဲ့ ချိတ်တယ်။
-p 5432- TCP port 5432 ကို သုံးတယ်။
-U nyinyizin- nyinyizin login role နဲ့ ဝင်တယ်။
-d mydb- mydb database ကို တန်းချိတ်တယ်။
Role အသစ်ပိုင်တဲ့ Table တစ်ခု ဆောက်ခြင်း
mydb ထဲမှာ connection စစ်ပြီး nyinyizin နဲ့ စမ်းသပ် table တစ်ခု ဆောက်ပါ။ ဆောက်တဲ့ role က ဒီ table ရဲ့ owner ဖြစ်တယ်။ \dt နဲ့ owner စစ်ပြီး \q နဲ့ ထွက်ပါ။
\conninfo
SELECT current_user, current_database();
CREATE TABLE public.role_demo (id integer PRIMARY KEY);
\dt public.role_demo
\q
mydb ကို postgres နဲ့ ပြန်ဝင်ခြင်း
Owner ပြန်ပြောင်းပြီး စမ်းသပ် role ကို မဖျက်ခင် OS terminal မှာ ဒီ command ရိုက်ပါ။
psql -h localhost -p 5432 -U postgres -d mydb
Owner ပြန်ပြောင်းပြီး Login Role ဖျက်ခြင်း
nyinyizin session ကနေ ထွက်ပြီးရင် mydb ထဲကို postgres နဲ့ ဝင်ထားချိန် ဒီ command တွေ run ပါ။ ALTER DATABASE က database owner ကို ပြန်ပြောင်းတယ်။ REASSIGN OWNED က role_demo အပါအဝင် စမ်းသပ် role ပိုင်နေသေးတဲ့ objects တွေကို လွှဲပေးတယ်။ Role မဖျက်ခင် table owner ကို စစ်ပါ။
ALTER DATABASE mydb OWNER TO postgres;
REASSIGN OWNED BY nyinyizin TO postgres;
\dt public.role_demo
DROP USER nyinyizin;
\du nyinyizin
REASSIGN OWNED- လက်ရှိ database ထဲက source role ပိုင် objects တွေနဲ့ databases၊ tablespaces လို shared objects တွေရဲ့ owner ကို ပြောင်းတယ်။ ဒီဥပမာမှာ source က အခုမှ ဆောက်ထားတဲ့ စမ်းသပ် role ပါ။
DROP USER- Role ကို ဖျက်တယ်။ ဒီ role ကို မှီနေတဲ့ database objects သို့မဟုတ် privileges ရှိရင် PostgreSQL က ဖျက်ခွင့်မပေးဘူး။ လေ့ကျင့်ခန်းပြင်ပမှာ ဒီလိုဖြစ်ရင် ပြထားတဲ့ dependencies တွေကို စစ်ပြီးမှ ပြန်စမ်းပါ။