環境:
centos 7 + pg10.17
(一)安裝包下載
postgresql數據庫一共有2種安裝包,一種是rpm包,可以直接使用yum安裝,另外一種是源碼,需要自己編譯安裝,可以看到,與MySQL相比,少了二進制的安裝包,這裏我們使用源碼自己編譯安裝。
首先下載源碼安裝包,各版本的下載地址:https://www.postgresql.org/ftp/source
下載自己需要的版本即可
(二)安裝PostgreSQL
(2.1)解壓安裝包
[root@pg01 tmp]# pwd /tmp [root@pg01 tmp]# ls postgresql-10.17.tar.gz [root@pg01 tmp]# tar -xzvf postgresql-10.17.tar.gz [root@pg01 tmp]# ls postgresql-10.17 postgresql-10.17.tar.gz
(2.2)配置
./configure \ --prefix=/pg \ --exec-prefix=/pg \ --bindir=/pg/bin \ --sysconfdir=/pg/etc \ --libdir=/pg/lib \ --includedir=/pg/include --mandir=/pg/man --docdir=/pg/doc \ --htmldir=/pg/html \ --enable-nls='zh_CN en_US' \ --with-pgport=5432 \ --with-perl \ --with-python \ --with-tcl \ --with-icu \ --with-openssl \ --with-pam \ --with-ldap \ --with-systemd \ --with-readline \ --with-libxml \ --with-libxslt \ --with-segsize=1 \ --with-blocksize=8 \ --with-wal-segsize=16 \ --with-wal-blocksize=8 \ --without-zlib
配置的過程中,會提示缺少包,需要根據提示,手動安裝這些包
# 查找 yum list all|grep readline # 安裝 yum install -y gcc yum install -y libicu-devel.x86_64 yum install -y perl-ExtUtils-Embed.noarch yum install -y readline-devel.x86_64 yum -y install openssl-devel yum -y install pam-devel.x86_64 yum install -y libxml2-devel.x86_64 yum install -y libxslt.x86_64 yum install -y libxslt-devel.x86_64 yum install -y openldap-devel.x86_64 yum install -y systemd-devel.x86_64 yum install -y tcl-devel.x86_64 yum install -y python-devel.x86_64
(2.3)編譯安裝
(2.3.1)編譯
首先進行編譯,使用make命令進行編譯,如果希望編譯所有的東西,包括文檔(man、html)和附加模塊(contrib),使用
make world
如果最後1行出現下面的描述,說明編譯成功
PostgreSQL, contrib, and documentation successfully made. Ready to install.
(2.3.2)安裝
要安裝PostgreSQL,輸入下面命令進行安裝
make install
當最後1行出現"PostgreSQL installation complete."時,說明安裝成功了。
NOTE:這條命令會把文件安裝到2.2指定的路徑,需要確保有足夠的權限向該區域寫入。通常需要使用root權限操作,或者也可以事先創建目錄並分配權限給相應的用戶
(2.4)創建pg用戶
adduser postgres passwd postgres mkdir -p /pg/data chown -R postgres:postgres /pg/
(2.5)初始化數據庫
su - postgres # 初始化 /pg/bin/initdb -D /pg/data # 或者 /pg/bin/pg_ctl -D /pg/data initdb
(2.6)設置環境變量
在postgres用戶下配置pg數據庫的環境變量
[postgres@pg01 ~]$ cat .bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/.local/bin:$HOME/bin:/pg/bin export PATH export PGHOME=/pg export PGDATA=/pg/data export LD_LIBRARY_PATH=/pg/lib export MANPATH=/pg/man export PATH=/pg/bin:$PATH [postgres@pg01 ~]$
使用source命令生效
[postgres@pg01 ~]$ source .bash_profile
(三)啓動與關閉
(3.1)在postgresql用戶下使用pg_ctl
# 1.啓動數據庫 /pg/bin/postgres -D /pg/data >logfile 2>&1 & # 或者可以使用pg_ctl命令 /pg/bin/pg_ctl -D /pg/data -l /tmp/logfile start # 2.關閉數據庫 /pg/bin/pg_ctl -D /pg/data -l /tmp/logfile stop # 3.查看數據庫狀態 /pg/bin/pg_ctl -D /pg/data status
(3.2)使用root配置systemd管理數據庫
# 1.使用root用戶配置 su - root vim /etc/systemd/system/postgresql.service [Unit] Description=PostgreSQL database server Documentation=man:postgres(1) [Service] Type=notify User=postgres ExecStart=/pg/bin/postgres -D /pg/data ExecReload=/bin/kill -HUP $MAINPID KillMode=mixed KillSignal=SIGINT TimeoutSec=0 [Install] WantedBy=multi-user.target # 2.重新加載systemd配置 [root@pg01 pg]# systemctl daemon-reload # 3.啓動、關閉、狀態,使用root用戶 systemctl start postgresql systemctl stop postgresql systemctl status postgresql # 4.配置開機自啓動 systemctl enable postgresql
【完】