Postgresql 源码安装-Linux

来源:这里教程网 时间:2026-03-14 20:16:51 作者:

-- 创建用户

groupadd -g 6000 pgsql
useradd -u 6000 -g pgsql pgsql
echo "123456" | passwd --stdin pgsql

-- 创建目录

mkdir -p /postgresql/{pgdata,archive,scripts,backup,pg13,soft}
chown -R pgsql:pgsql/postgresql
chmod -R 775 /postgresql

--安装依赖包

yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel  python-devel gcc-c++ openssl-devel cmake

-- 编译

su - pgsql
cd /postgresql/soft
tar -zxvf postgresql-13.3.tar.gz 
$ cd postgresql-13.3/
$ ./configure --prefix=/postgresql/pg13 --without-readline
$ make -j 8 && make install

-- 配置环境变量

$ cat >>  ~/.bash_profile <<"EOF"
export.UTF-8
export PS1="[\u@\h \W]\$ "
export PGPORT=5432
export PGDATA=/postgresql/pgdata
export PGHOME=/postgresql/pg13
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH:.
export DATE=`date +"%Y%m%d%H%M"`
export MANPATH=$PGHOME/share/man:$MANPATH
export PGHOST=$PGDATA
export PGUSER=postgres
export PGDATABASE=postgres
#alias psql='rlwrap psql' 
EOF
source  ~/.bash_profile

-- 初始化

su - pgsql
$ /postgresql/pg13/bin/initdb -D /postgresql/pgdata -E UTF8 --locale=en_US.utf8 -U postgres
  Success. You can now start the database servce using:
      pg_ctl -D /postgresql/pgdata  -l logfile start

相关配置文件:

/postgresql/pgdata/postgresql.conf.  /postgresql/pgdata/pg_hba.conf

-- 修改参数

$ cat >> /postgresql/pgdata/postgresql.conf <<"EOF"
listen_addresses = '*'
port=5432
unix_socket_directories='/postgresql/pgdata'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%a.log'
#log_filename = 'postgresql-%Y-%m-%d_%H-%M%S.log'
log_truncate_on_rotation = on
EOF
$ cat   > /postgresql/pgdata/pg_hba.conf << EOF
# TYPE  DATABASE    USER    ADDRESS       METHOD
local     all       all                    trust
host      all       all   127.0.0.1/32     trust
host      all       all    0.0.0.0/0        md5
host   replication  all    0.0.0.0/0        md5
EOF

日志相关参数:logging_collector            打开日志采集log_directory                  设置日志路径(也可以是绝对路径)log_filename                  日志文件名称 格式数据库监听参数:listen_address               localhost 只能本地登陆,也可以是实际地址用',' 分割。port                                数据库端口 -- 启动、停止、查看状态

pg_ctl start [-D DATADIR]
pg_ctl status [-D DATADIR]
pg_ctl stop [-D DATADIR][-m SHUTDOWN-MODE]
--启动数据库
$ pg_ctl start -D /postgresql/pgdata
server starting
LOG:redirecting log output to logging collector process
HINT:Future log output will appear in directory 'pg_log'
或者
/postgresql/pg13/bin/postgres -D /postgresql/pgdata 
--查看数据库状态
$ pg_ctl status -D /postgresql/pgdata 
pg_ctl:server is running (PID:24839)
/postgresql/pg13/bin/postgres "-D" "/postgresql/pgdata"
--停止数据库
$ pg_ctl stop -D /postgresql/pgdata
waiting for server to shut down... done 
server stopped 
-m 数据库的停止方式:
smart: 等待所有连接终止后停止数据库
fast: 快速断开链接停止数据库
immediate:立刻关闭数据库,但是下次启动时需要恢复。
默认是fast 停止。

-- 配置系统服务(systemctl)

cat > /etc/systemd/system/PG13.service <<"EOF"
[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)
After=network.target
[Service]
Type=forking
User=pgsql
Group=pgsql
Environment=PGPORT=5432
Environment=PGDATA=/postgresql/pgdata
OOMScoreAdjust=-1000
ExecStart=/postgresql/pg13/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300
ExecStop=/postgresql/pg13/bin/pg_ctl stop -D ${PGDATA} -s -m fast
ExecReload=/postgresql/pg13/bin/pg_ctl reload -D ${PGDATA} -s
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=0
[Install]
WantedBy=multi-user.target
EOF
配置完成 可以使用systemctl 启动和停止数据库。
systemctl daemon-reload
systemctl enable PG13
systemctl start PG13
systemctl status PG13

安装rlwrap软件:

# yum install -y gcc libtermcap-devel readline readline-devel
# cd /postgresql/soft/
# ll | grep rlwrap-0.37.tar.gz
# tar -zxvf rlwrap-0.37.tar.gz  
# cd rlwrap-0.37
# ./configure; make && make install
......
make[3]: Leaving directory `/soft/rlwrap-0.37'
make[2]: Leaving directory `/soft/rlwrap-0.37'
make[1]: Leaving directory `/soft/rlwrap-0.37'
# ldconfig

相关推荐