环境:
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
【完】
