psql နဲ့ ချိတ်ပြီး Tables တည်ဆောက်ခြင်း
PostgreSQL 19.0
အရင်သင်ခန်းစာမှာ ထည့်ထားတဲ့ server ကို ဖွင့်ထားပါ။ ကိုယ့် OS အတွက် connection command ကို ရွေးပြီး practice database ဆောက်မယ်။ ပြီးရင် students၊ courses နဲ့ enrollments tables တွေ ဆောက်ကြည့်မယ်။
Code စမ်းလို့ရတဲ့ သင်ခန်းစာ ဖွင့်ရန်
ဒီစာမျက်နှာမှာ
- Windows PowerShell ကနေ psql ချိတ်ခြင်း
- Postgres.app သုံးထားတဲ့ macOS ကနေ ချိတ်ခြင်း
- Ubuntu မှာ Local postgres Account နဲ့ ချိတ်ခြင်း
- postgres နဲ့ အတိုကောက် ဝင်ခြင်း
- psql Prompt ကို ဖတ်ခြင်း
- လေ့ကျင့်ဖို့ Database တစ်ခု ဆောက်ခြင်း
- Students၊ Courses နဲ့ Enrollments Tables ဆောက်ခြင်း
- တရားဝင် ကိုးကားချက်များ
Windows PowerShell ကနေ psql ချိတ်ခြင်း
PowerShell မှာ ရိုက်ပါ။ Password တောင်းရင် install တုန်းက သတ်မှတ်ထားတဲ့ PostgreSQL password ကို ထည့်ပါ။ Port သို့မဟုတ် role မတူရင် သက်ဆိုင်တဲ့တန်ဖိုးကို ပြောင်းပါ။
psql -h localhost -p 5432 -U postgres -d postgres
psql- PostgreSQL terminal client ကို ဖွင့်တယ်။
-h localhost- ဘယ်စက်ကို ချိတ်မလဲဆိုတာ ရွေးတယ်။ localhost က psql run နေတဲ့ ကိုယ့်စက်ပါ။
-p 5432- အဲဒီစက်ပေါ်က TCP port ကို ရွေးတယ်။
-U postgres- postgres ဆိုတဲ့ PostgreSQL role နဲ့ ဝင်တယ်။
-d postgres- postgres ဆိုတဲ့ database ကို ဖွင့်တယ်။ ဒီဥပမာမှာ role နာမည်နဲ့ database နာမည်က တူနေတယ်။
စမ်းကြည့်ရင် ဘာဖြစ်မလဲ postgres=# လို prompt ပေါ်ရင် psql ထဲ ရောက်နေပါပြီ။ Password ရိုက်နေချိန် စာလုံးတွေ မပေါ်တာ ပုံမှန်ပါ။
Postgres.app သုံးထားတဲ့ macOS ကနေ ချိတ်ခြင်း
Terminal မှာ Postgres.app က သတ်မှတ်ပေးထားတဲ့ macOS account နာမည်ကို role အဖြစ် သုံးပါ။ Shell က whoami ကို run လုပ်ပြီး account နာမည် ထည့်ပေးတယ်။ Postgres.app server ရဲ့ port က မတူရင် ပြောင်းပါ။
psql -h localhost -p 5432 -U "$(whoami)" -d postgres
Ubuntu မှာ Local postgres Account နဲ့ ချိတ်ခြင်း
ပုံမှန် Ubuntu package installation မှာ local Unix socket ကနေ ချိတ်ပါတယ်။ sudo -u postgres က psql ကို OS ရဲ့ postgres user အဖြစ် run ပေးတယ်။ Peer authentication က အဲဒီ OS user နာမည်ကို PostgreSQL role နာမည်နဲ့ တိုက်စစ်နိုင်တယ်။ -d က postgres database ကို ရွေးတယ်။
sudo -u postgres psql -d postgres
postgres နဲ့ အတိုကောက် ဝင်ခြင်း
OS terminal မှာ ရိုက်ပါ။ postgres role ကို ရွေးပေးတယ်။ Client ရဲ့ default settings နဲ့ဆို psql က postgres database၊ local connection နဲ့ port 5432 ကို သုံးတယ်။ Local authentication နဲ့ ဝင်မရရင် အပေါ်က ကိုယ့် OS အတွက် command အပြည့်အစုံကို သုံးပါ။
psql -U postgres
psql Prompt ကို ဖတ်ခြင်း
Prompt ကို program က ပြပေးတာပါ။ Prompt နောက်မှာ command ကိုပဲ ရိုက်ပါ။ psql ထဲမှာ terminal command ကို မှားရိုက်မိလို့ continuation prompt ဖြစ်သွားရင် အောက်ကအတိုင်း ပြန်ရှင်းပါ။
postgres=# SELECT
postgres-# CURRENT_DATE;
current_date
--------------
2026-11-24
(1 row)
postgres=#- postgres database ထဲမှာ command လက်ခံဖို့ အသင့်ဖြစ်နေတယ်။ Default prompt မှာ # က ပုံမှန်အားဖြင့် superuser ကို ဆိုလိုတယ်။ သာမန် role ဆို postgres=> လို့ မြင်ရတယ်။
postgres-#- SQL statement မပြီးသေးတာပါ။ အဆုံးမှာ semicolon မထည့်ရသေးလို့ ဖြစ်တာများတယ်။
\r / Ctrl+C- ရိုက်လက်စ statement ကို \r နဲ့ ရှင်းပါ။ Quote မပိတ်ဘဲ ကျန်နေရင် Ctrl+C နှိပ်ပါ။ OS terminal command ပြန်ရိုက်ဖို့ \q နဲ့ psql ထဲက ထွက်ပါ။
လေ့ကျင့်ဖို့ Database တစ်ခု ဆောက်ခြင်း
psql ထဲမှာ database ဆောက်ခွင့်ရှိတဲ့ role နဲ့ CREATE DATABASE ကို run ပါ။ ပြီးရင် client ထဲက မထွက်ဘဲ database ပြောင်းဖို့ psql command ဖြစ်တဲ့ \c ကို သုံးပါ။ နောက်သင်ခန်းစာတွေကို ဒီ database ထဲမှာ စမ်းပါ။
CREATE DATABASE learning_db;
\c learning_db
စမ်းကြည့်ရင် ဘာဖြစ်မလဲ CREATE DATABASE လို့ ပြပြီး learning_db ကို ချိတ်ထားကြောင်း ပေါ်မယ်။ Prompt က learning_db နဲ့ စမယ်။
ရလဒ်ကွာနေရင် learning_db ရှိပြီးသားဆို \c learning_db ပဲ run ပါ။ connection refused ဆိုရင် ရွေးထားတဲ့ address မှာ server လက်ခံနေလား စစ်ပါ။ password authentication failed ဆိုရင် ချိတ်ထားတဲ့ server အတွက် role နဲ့ password ကို ပြန်စစ်ပါ။
Students၊ Courses နဲ့ Enrollments Tables ဆောက်ခြင်း
learning_db ထဲမှာ run ပါ။ Enrollment row တစ်ကြောင်းက student တစ်ယောက်နဲ့ course တစ်ခုကို ချိတ်ပါတယ်။ Student တစ်ယောက်က course အများကြီး တက်နိုင်သလို course တစ်ခုမှာလည်း student အများကြီး ရှိနိုင်တယ်။ Parent table နှစ်ခုကို အရင်ဆောက်ပြီးမှ enrollments ဆောက်ပါ။ အရင်သင်ခန်းစာက learning_db ရှိပြီးသားဆိုရင်လည်း ဒီ block တစ်ခုလုံး run လို့ရပါတယ်။
CREATE TABLE IF NOT EXISTS students (
id integer PRIMARY KEY,
name text NOT NULL
);
CREATE TABLE IF NOT EXISTS courses (
id integer PRIMARY KEY,
title text NOT NULL,
fee numeric(10, 2) NOT NULL CHECK (fee >= 0)
);
CREATE TABLE IF NOT EXISTS enrollments (
student_id integer REFERENCES students(id),
course_id integer REFERENCES courses(id),
enrolled_on date NOT NULL,
PRIMARY KEY (student_id, course_id)
);
INSERT INTO students (id, name)
VALUES (1, 'Aye'), (2, 'Min'), (3, 'Su')
ON CONFLICT (id) DO NOTHING;
INSERT INTO courses (id, title, fee)
VALUES (1, 'SQL Basics', 30.00), (2, 'Web Development', 25.00),
(3, 'Computer Fundamentals', 20.00)
ON CONFLICT (id) DO NOTHING;
INSERT INTO enrollments (student_id, course_id, enrolled_on)
VALUES (1, 1, DATE '2026-01-05'), (2, 1, DATE '2026-01-06'),
(1, 2, DATE '2026-01-07')
ON CONFLICT (student_id, course_id) DO NOTHING;
students / courses- Parent table နှစ်ခုက student နာမည်နဲ့ course အချက်အလက်တွေကို သိမ်းတယ်။ Table တစ်ခုစီရဲ့ ID က သူ့ primary key ပါ။
enrollments / REFERENCES- Foreign key နှစ်ခုကြောင့် ID နှစ်ခုလုံး သက်ဆိုင်ရာ table မှာ ရှိရတယ်။ Column နှစ်ခုပေါင်းထားတဲ့ primary key က student တစ်ယောက် course တစ်ခုတည်းမှာ နှစ်ခါစာရင်းသွင်းတာကို တားတယ်။
IF NOT EXISTS / ON CONFLICT- ထပ် run ရင် ရှိပြီးသား table တွေကို ဆက်သုံးပြီး key တူတဲ့ နမူနာ row တွေကို ကျော်သွားတယ်။
စမ်းကြည့်ရင် ဘာဖြစ်မလဲ နမူနာအသစ်မှာ student သုံးယောက်၊ course သုံးခုနဲ့ enrollment သုံးကြောင်း ရှိမယ်။ Aye က course နှစ်ခု တက်ပြီး Computer Fundamentals မှာ စာရင်းသွင်းသူ မရှိသေးဘူး။