-
Notifications
You must be signed in to change notification settings - Fork 3
Install & Uninstall
Tip: หากต้องการติดตั้ง PostgreSQL ในระบบปฏิบัติการอื่นๆ ดูเพิ่มเติมที่ PostgreSQL Download หรือ PostgreSQL Wiki
คู่มือนี้ครอบคลุมการติดตั้งและกำหนดค่า PostgreSQL Database Server & Client บน 👇 ระบบปฏิบัติการต่อไปนี้
- Ubuntu and Debian (Linux)
- Red Hat Enterprise, Rocky, or Oracle version 9, 8 (Linux)
- CentOS 6, Red Hat Enterprise, Scientific or Oracle version 7 (Linux)
- CentOS 7, Red Hat Enterprise, Scientific or Oracle version 6 (Linux)
- Fedora version 37, 36 (Linux)
- Ubuntu 22.04 LTS
- Ubuntu 20.04 LTS
- Ubuntu 18.04 LTS
- Debian 11
- Debian 10
- Debian 9
- เช็คว่ามี PostgreSQL Package ว่าเป็นเวอร์ชั้นที่เราต้องการหรือไม่
sudo apt-cache search postgresql | grep postgresql
- Add PostgreSQL Package Repository (หากไม่มีให้เพิ่มเข้าไป)
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget -qO- https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo tee /etc/apt/trusted.gpg.d/pgdg.asc &>/dev/null
sudo apt update -y
Note:
apt install -y postgresql
คือเครื่องที่เราจะให้เป็น Database Server และ ส่วนเครื่องที่ไม่ได้เป็น Database Server หรือ เครื่องที่เป็น Client จะไม่สามารถใช้คำสั่ง เช่นpsql -U postgresql -h <ip-server-database> -p <port>
เพื่อเชื่อมต่อไปยัง Database Server จะไม่สามารถใช้คำสั่งนี้ได้เนื่องต้องapt install -y postgresql-client
ก่อนถึงจะสามารถใช้ได้
(15.1) Latest Version
Server
sudo apt install -y postgresql
Client
sudo apt install -y postgresql-client
Other version (1-14)
(14.0) Server
sudo apt install -y postgresql-14
Client
sudo apt install -y postgresql-client-14
(13.4)
Server
sudo apt install -y postgresql-13
Client
sudo apt install -y postgresql-client-13
(12.8)
Server
sudo apt install -y postgresql-12
Client
sudo apt install -y postgresql-client-12
(11.13)
Server
sudo apt install -y postgresql-11
Client
sudo apt install -y postgresql-client-11
(10.18)
Server
sudo apt install -y postgresql-10
Client
sudo apt install -y postgresql-client-10
(9.6.23) (!) Unable to cluster not have pg_upgradecluster
!
Client
sudo apt install -y postgresql-9.6
Server
sudo apt install -y postgresql-client-9.6
(9.5.27) (!) Unable to cluster not have pg_upgradecluster
!
Client
sudo apt install -y postgresql-9.5
Server
sudo apt install -y postgresql-client-9.5
(9.4.32) (!) Unable to cluster not have pg_upgradecluster
!
Client
sudo apt install -y postgresql-9.4
Server
sudo apt install -y postgresql-client-9.4
(9.3.27) (!) Unable to cluster not have pg_upgradecluster
!
Client
sudo apt install -y postgresql-9.3
Server
sudo apt install -y postgresql-client-9.3
(9.2.24) (!) Unable to cluster not have pg_upgradecluster
!
Client
sudo apt install -y postgresql-9.2
Server
sudo apt install -y postgresql-client-9.2
(9.1.24) (!) Unable to cluster not have pg_upgradecluster
!
Client
sudo apt install -y postgresql-9.1
Server
sudo apt install -y postgresql-client-9.1
(9.0.24) (!) Unable to cluster not have pg_upgradecluster
!
Client
sudo apt install -y postgresql-9.0
Server
sudo apt install -y postgresql-client-9.0
(8.4.23) (!) Unable to cluster not have pg_upgradecluster
!
Client
sudo apt install -y postgresql-8.4
Server
sudo apt install -y postgresql-client-8.4
(8.3.23) (!) Unable to cluster not have pg_upgradecluster
!
Client
sudo apt install -y postgresql-8.3
Server
sudo apt install -y postgresql-client-8.3
(8.2.23) (!) Unable to cluster not have pg_upgradecluster
!
Client
sudo apt install -y postgresql-8.2
Server
sudo apt install -y postgresql-client-8.2
sudo systemctl enable postgresql
sudo systemctl start postgresql
systemctl status postgresql
sudo -u postgres psql -c "SELECT version();"
พิมแล้วพิม :q เพื่อออก จาก ☝
OR:
psql --version
ตั้งค่าให้ PostgreSQL รับการเชื่อมต่อจากภายนอกเครื่อง โดยการแก้ไขไฟล์ pg_hba.conf
ดังนี้
vi /etc/postgresql/<VERSION>/main/pg_hba.conf
-
<VERSION> คือเวอร์ชั้น PostgreSQL ที่ติดตั้ง เช่น หากเป็น
15
sudo vi /etc/postgresql/15/main/pg_hba.conf
เพิ่มบรรทัดใหม่นี้ที่ pg_hba.conf
config file
host all all 0.0.0.0/0 md5
เพื่อให้ PostgreSQL รับการเชื่อมต่อจากทุกเครื่อง โดยใช้รหัสผ่านเข้ารหัสด้วย MD5
เพื่อให้สามารถเข้าจากเครื่องอื่นๆ หรือ clinet ตั้ง listen_addresses
ให้เป็น *
หรือ <interface-ip-address>
ที่ไฟล์ postgresql.conf
โดยพิมคำสั่งดังนี้
sudo vi /etc/postgresql/15/main/postgresql.conf
Restart เพื่อให้ตั้งค่า take effect กับ config pg_hba.conf
sudo systemctl restart postgresql
ใช้คำสั่งต่อไปนี้เพื่อลบ PostgreSQL ออกจากเครื่องทั้งหมด
sudo apt-get --purge remove postgresql postgresql-* -y && sudo rm -rf /var/lib/postgresql/ && sudo rm -rf /var/log/postgresql/ && sudo rm -rf /etc/postgresql/
- Red Hat Enterprise Linux 9
- Rocky Linux 9
- Oracle Linux 9
- Red Hat Enterprise Linux 8
- Rocky Linux 8
- Oracle Linux 8
- Add PostgreSQL Package Repository (หากไม่มีให้เพิ่มเข้าไป)
Red Hat Enterprise, Rocky, or Oracle 9
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Red Hat Enterprise, Rocky, or Oracle 8
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo dnf -qy module disable postgresql
(15.1) Latest Version
sudo dnf install -y postgresql15-server postgresql15-client
sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
sudo systemctl enable postgresql-15
sudo systemctl start postgresql-15
systemctl status postgresql-15
(14.0) Old Version
sudo dnf install -y postgresql14-server postgresql14-client
sudo /usr/pgsql-14/bin/postgresql-14-setup initdb
sudo systemctl enable postgresql-14
sudo systemctl start postgresql-14
systemctl status postgresql-14
(13.4) Old Version
sudo dnf install -y postgresql13-server postgresql13-client
sudo /usr/pgsql-13/bin/postgresql-13-setup initdb
sudo systemctl enable postgresql-13
sudo systemctl start postgresql-13
systemctl status postgresql-13
(12.8) Old Version
sudo dnf install -y postgresql12-server postgresql12-client
sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
sudo systemctl enable postgresql-12
sudo systemctl start postgresql-12
systemctl status postgresql-12
(11.13) Old Version
sudo dnf install -y postgresql11-server postgresql11-client
sudo /usr/pgsql-11/bin/postgresql-11-setup initdb
sudo systemctl enable postgresql-11
sudo systemctl start postgresql-11
systemctl status postgresql-11
sudo -u postgres psql -c "SELECT version();"
OR:
psql --version
- Red Hat Enterprise Linux 7
- CentOS 7
- Scientific Linux 7
- Oracle Linux 7
- Add PostgreSQL Package Repository (หากไม่มีให้เพิ่มเข้าไป)
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
(15.1) Latest Version
sudo yum install -y postgresql15-server
sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
sudo systemctl enable postgresql-15
sudo systemctl start postgresql-15
systemctl status postgresql-15
(14.0) Old Version
sudo yum install -y postgresql14-server
sudo /usr/pgsql-14/bin/postgresql-14-setup initdb
sudo systemctl enable postgresql-14
sudo systemctl start postgresql-14
systemctl status postgresql-14
(13.4) Old Version
sudo yum install -y postgresql13-server
sudo /usr/pgsql-13/bin/postgresql-13-setup initdb
sudo systemctl enable postgresql-13
sudo systemctl start postgresql-13
systemctl status postgresql-13
(12.8) Old Version
sudo yum install -y postgresql12-server
sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
sudo systemctl enable postgresql-12
sudo systemctl start postgresql-12
systemctl status postgresql-12
(11.13) Old Version
sudo yum install -y postgresql11-server
sudo /usr/pgsql-11/bin/postgresql-11-setup initdb
sudo systemctl enable postgresql-11
sudo systemctl start postgresql-11
systemctl status postgresql-11
sudo -u postgres psql -c "SELECT version();"
OR:
psql --version
- Red Hat Enterprise Linux 6
- CentOS 6
- Scientific Linux 6
- Oracle Linux 6
- Add PostgreSQL Package Repository (หากไม่มีให้เพิ่มเข้าไป)
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-6-x86_64/pgdg-redhat-repo-latest.noarch.rpm
(15.1) Latest Version
sudo yum install -y postgresql15-server
sudo service postgresql-15 initdb
sudo chkconfig postgresql-15 on
sudo service postgresql-15 start
systemctl status postgresql-15
(14.0) Old Version
sudo yum install -y postgresql14-server
sudo service postgresql-14 initdb
sudo chkconfig postgresql-14 on
sudo service postgresql-14 start
systemctl status postgresql-14
(13.4) Old Version
sudo yum install -y postgresql13-server
sudo service postgresql-13 initdb
sudo chkconfig postgresql-13 on
sudo service postgresql-13 start
systemctl status postgresql-13
(12.8) Old Version
sudo yum install -y postgresql12-server
sudo service postgresql-12 initdb
sudo chkconfig postgresql-12 on
sudo service postgresql-12 start
systemctl status postgresql-12
(11.13) Old Version
sudo yum install -y postgresql11-server
sudo service postgresql-11 initdb
sudo chkconfig postgresql-11 on
sudo service postgresql-11 start
systemctl status postgresql-11
sudo -u postgres psql -c "SELECT version();"
OR:
psql --version
- Fedora version 37
- Fedora version 36
- Add PostgreSQL Package Repository (หากไม่มีให้เพิ่มเข้าไป)
Fedora version 37
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/F-37-x86_64/pgdg-fedora-repo-latest.noarch.rpm
Fedora version 36
sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/F-36-x86_64/pgdg-fedora-repo-latest.noarch.rpm
(15.1) Latest Version
sudo dnf install -y postgresql15-server postgresql15-client
sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
sudo systemctl enable postgresql-15
sudo systemctl start postgresql-15
systemctl status postgresql-15
(14.0) Old Version
sudo dnf install -y postgresql14-server postgresql14-client
sudo /usr/pgsql-14/bin/postgresql-14-setup initdb
sudo systemctl enable postgresql-14
sudo systemctl start postgresql-14
systemctl status postgresql-14
(13.4) Old Version
sudo dnf install -y postgresql13-server postgresql13-client
sudo /usr/pgsql-13/bin/postgresql-13-setup initdb
sudo systemctl enable postgresql-13
sudo systemctl start postgresql-13
systemctl status postgresql-13
(12.8) Old Version
sudo dnf install -y postgresql12-server postgresql12-client
sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
sudo systemctl enable postgresql-12
sudo systemctl start postgresql-12
systemctl status postgresql-12
(11.13) Old Version
sudo dnf install -y postgresql11-server postgresql11-client
sudo /usr/pgsql-11/bin/postgresql-11-setup initdb
sudo systemctl enable postgresql-11
sudo systemctl start postgresql-11
systemctl status postgresql-11
sudo -u postgres psql -c "SELECT version();"
OR:
psql --version
ww