PostgreSQL是最先進的開源數據庫。PostgreSQL簡稱pgsql,目前最新版本是12.1,
訪問PostgreSQL官網https://www.postgresql.org/download/,可以下載源碼,也可以用二進制包安裝,這裏使用二進制包安裝。
選擇操作系統,選擇Red Hat family Linux (including CentOS/Fedora/Scientific/Oracle variants):
點擊進入後,選擇pgsql版本,這裏選擇11,操作系統爲CentOS7,x86_64,然後就會有相關信息:
以下在需要安裝pgsql的系統中使用root用戶執行:
執行:
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
完畢後重新生成本地yum緩存:
yum clean all
yum makecache
安裝(postgresql11爲客戶端,postgresql11-server爲服務端,postgresql11-devel爲一些庫可以不裝):
yum install postgresql11 postgresql11-server postgresql11-devel -y
初始化數據庫:
官網默認的是
/usr/pgsql-11/bin/postgresql-11-setup initdb
這樣初始化的數據庫編碼是英文的,咋們是中國人,肯定是要用中文的編碼啦,
正確的數據庫初始化命令是:
sudo su -c " PGSETUP_INITDB_OPTIONS='-U postgres -E UTF8 --locale=zh_CN.UTF-8' /usr/pgsql-11/bin/postgresql-11-setup initdb"
設置開機啓動和啓動服務:
設置密碼(安裝完畢默認沒有密碼):
使用以下命令登錄數據庫:
sudo -u postgres psql postgres
登錄之後:
輸入(注意不能少了反斜槓\):
\password postgres
編輯配置文件/var/lib/pgsql/11/data/pg_hba.conf修改成密碼登錄:
vim /var/lib/pgsql/11/data/pg_hba.conf
將peer和ident 修改爲md5
執行以下命令重啓服務:
systemctl restart postgresql-11
至此PostgreSQL11安裝完畢。
其他系統Debian、Ubuntu等安裝類似。