เนื่องจาก debian 11 มัน default package เป็น postgres 13 แต่เราอยากจะติดตั้ง 14 ไง งั้นติดตั้ง package ที่จำเป็นก่อน
apt -y install gnupg2
echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
จากนั้นเราก็มี postgres-14 ให้ติดตั้งแล้ว ก็ดำเนินการติดตั้งไป ก็จบ
apt update
apt -y install postgresql-14
ถ้าต้องการจะให้ connect จากเครื่องอื่นได้ ที่ต้องแก้ ก็จะมีไฟล์ /etc/postgresql/14/main/postgresql.conf
แก้
listen_addresses = '*' # -> เดิมคือ listen_addresses = 'localhost'
จากนั้นก็แก้ /etc/postgresql/14/main/pg_hba.conf
ด้วยการเพิ่มบรรทัดด้านล่างนี้เข้าไป
host all all 0.0.0.0/0 md5
จากนั้น restart ด้วยคำสั่ง
pg_ctlcluster 14 main restart
ปัญหาใหญ่ของเรื่องนี้ คือแล้วไอ้ user/password default มันคืออะไร ไม่เห็นจะมีอะไรให้ตั้งค่า (เหมือน mysql) สรุปว่า ให้เข้า user postgres ก่อน แบบนี้
su - postgres
จากนั้นลองคำสั่ง โดยไม่ต้อง กังวล user/password เลย
$ psql -c "SELECT version();"
version
-----------------------------------------------------------------------------------------------------------------------------
PostgreSQL 14.2 (Debian 14.2-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit
(1 row)
จากนั้น เราก็สร้าง user/pass ตอนนี้ล่ะ ด้วยคำสั่ง createuser
$ createuser --interactive --pwprompt
Enter name of role to add: unno
Enter password for new role:
Enter it again:
Shall the new role be a superuser? (y/n) y
จากนั้น ก็ลองออกจาก user postgres แล้วใช้คำสั่งด้วย user ที่เราสร้าง แบบนี้
psql -h localhost -d postgres -U unno -W -c "SELECT version();"
มันก็ควรได้ผลแบบ ด้านบนนั่นล่ะ