安装环境:CentOS x64 安装 postgresql-12.3
yum install -y perl-ExtUtils-Embed yum install -y readline-devel yum install -y zlib-devel yum install -y pam-devel yum install -y libxml2-devel yum install -y libxslt-devel yum install -y openldap-devel yum install -y python-devel yum install -y gcc-c++ yum install -y openssl-devel yum install -y cmake
tar xf postgresql-12.3.tar.gz cd postgresql-12.3/ ./configure --prefix=/opt/postgresql make -j4 make install
./configure 可选参数:
安装后检查安装目录
[root@bogon postgresql-12.3]# cd /opt/postgresql/ [root@bogon postgresql]# ll 总用量 16 drwxr-xr-x. 2 root root 4096 6月 24 15:38 bin drwxr-xr-x. 6 root root 4096 6月 24 15:38 include drwxr-xr-x. 4 root root 4096 6月 24 15:38 lib drwxr-xr-x. 6 root root 4096 6月 24 15:38 share
如果需要编译debug版本,可以这样configure:
./configure --enable-cassert --enable-debug --enable-dtrace --prefix=/opt/postgresql CFLAGS="-ggdb -O0"
groupadd postgres
useradd -g postgres postgres
[root@bogon postgresql]# su - postgres [postgres@bogon ~]$ vim ~/.bash_profile # 添加: export PGHOME=/opt/postgresql export PGDATA=/opt/pgdata PATH=$PGHOME/bin:$PATH:$HOME/bin [postgres@bogon ~]$ source .bash_profile
[postgres@bogon ~]$ mkdir /opt/pgdata
[postgres@bogon ~]$ initdb /opt/pgdata The files belonging to this database system will be owned by user "postgres". This user must also own the server process. …… Success. You can now start the database server using: pg_ctl -D /opt/pgdata -l logfile start
#启动服务 [postgres@bogon ~]$ pg_ctl -D /opt/pgdata -l ~/logfile start #停止服务 [postgres@bogon ~]$ pg_ctl -D /opt/pgdata -l ~/logfile stop
PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下。
linux文件即为linux系统上的启动脚本
[postgres@localhost ~]$ cd /opt/postgresql-12.3/contrib/start-scripts [postgres@localhost start-scripts]$ ls freebsd linux macos
切换为root用户,修改linux文件属性,添加X属性
[root@localhost start-scripts]# chmod a+x linux
复制linux文件到/etc/init.d目录下,更名为postgresql
[root@localhost start-scripts]# cp linux /etc/init.d/postgresql
修改/etc/init.d/postgresql文件的两个变量
prefix设置为postgresql的安装路径:/pgsql/postgresql
PGDATA设置为postgresql的数据目录路径:/pgsql/postgresql/data
设置postgresql服务开机自启动
[root@localhost init.d]# chkconfig --add postgresql
查看开机自启动服务设置成功。
[root@localhost init.d]# chkconfig postgresql 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
5)编辑/etc/sysconfig/iptables文件开放5432端口。
[root@localhost sysconfig]# cd /etc/sysconfig [root@localhost sysconfig]# vi iptables # 添加以下内容 -A INPUT -p tcp -m tcp --dport 5432 -j ACCEPT
重启服务
[root@localhost sysconfig]# /etc/init.d/iptables restart
查看端口是否开放
[root@localhost sysconfig]# /sbin/iptables -L -n
执行service postgresql start,启动PostgreSQL服务
[root@localhost init.d]# service postgresql start