在容器内源码安装PostgreSQL13.2
环境参数:
linux版本:CentOS 7.6
PG版本:13.2
要先搭建好CentOS镜像,然后再开始。
[root@wcbpg ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wcbcentos76 1.0 6877e73a2018 2 weeks ago 1.9GB
wcbpg 1.0 988fc4d35414 2 weeks ago 1.9GB
centos 7.6.1810 f1cb7c7d58b7 2 years ago 202MB
#创建CentOS容器
[root@wcbpg ~]#docker run -d --name wcbpg -h wcbpg \
-p 15432-15439:5432-5439 \
-v /sys/fs/cgroup:/sys/fs/cgroup \
--privileged=true wcbcentos76:1.0 \
/usr/sbin/init
6c1a765e97e33bf69c1892c7403c0d39498dabff44903e7186b0c9307756b206
[root@wcbpg ~]# docker exec -it wcbpg bash
#在容器内安装依赖
[root@wcbpg /]# yum install -y cmake make gcc zlib gcc-c++ perl readline readline-devel zlib zlib-devel perl python36 tcl openssl ncurses-devel openldap pam
#删除已存在的PG,如果没有安装则不用删除
[root@wcbpg /]# yum remove -y postgresql* && rm -rf /var/lib/pgsql && rm -rf /usr/pgsql* && userdel -r postgres && groupdel postgres
[root@wcbpg /]# yum install -y sysbench
#创建用户
[root@wcbpg /]# groupadd -g 60000 pgsql
[root@wcbpg /]# useradd -u 60000 -g pgsql pgsql
[root@wcbpg /]# echo "wcb" | passwd --stdin pgsql
Changing password for user pgsql.
passwd: all authentication tokens updated successfully.
# 创建目录
[root@wcbpg /]# mkdir -p /postgresql/{pgdata,archive,scripts,backup,pg13,soft}
[root@wcbpg /]# chown -R pgsql:pgsql /postgresql
[root@wcbpg /]# chmod -R 775 /postgresql
#切换用户
[root@wcbpg /]# su - pgsql
[pgsql@wcbpg ~]$ cd /postgresql/soft
#用docker cp postgresql-13.2.tar.gz wcbpg:/ 把源码从Linux复制到docker内
[pgsql@wcbpg soft]$ cp /postgresql-13.2.tar.gz ./
[pgsql@wcbpg soft]$ ls -lth
total 27M
-rw-r--r-- 1 pgsql pgsql 27M Dec 16 23:07 postgresql-13.2.tar.gz
[pgsql@wcbpg soft]$ tar zxvf postgresql-13.2.tar.gz
[pgsql@wcbpg soft]$ cd postgresql-13.2
#检查
[pgsql@wcbpg postgresql-13.2]$ ./configure --prefix=/postgresql/pg13 --without-readline
#安装PG
[pgsql@wcbpg postgresql-13.2]$ make -j 8 && make install
#配置环境变量
[pgsql@wcbpg postgresql-13.2]$cat >> ~/.bash_profile <<"EOF"
export LANG=en_US.UTF-8
export PS1="[\u@\h \W]\$ "
export PGPORT=5433
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
#初始化
[pgsql@wcbpg postgresql-13.2]$ /postgresql/pg13/bin/initdb -D /postgresql/pgdata -E UTF8 --locale=en_US.utf8 -U postgres
#修改postgresql.conf文件
[pgsql@wcbpg postgresql-13.2]cat >> /postgresql/pgdata/postgresql.conf <<"EOF"
listen_addresses = '*'
port=5433
unix_socket_directories='/postgresql/pgdata'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%a.log'
log_truncate_on_rotation = on
EOF
#修改pg_hba.conf文件
[pgsql@wcbpg postgresql-13.2]cat > /postgresql/pgdata/pg_hba.conf << EOF
# TYPE DATABASE USER ADDRESS METHOD
local all all trust
host all all ::1/128 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
[pgsql@wcbpg postgresql-13.2]$ pg_ctl start
waiting for server to start....2021-12-16 23:18:21.382 CST [9238] LOG: redirecting log output to logging collector process
2021-12-16 23:18:21.382 CST [9238] HINT: Future log output will appear in directory "pg_log".
done
server started
[root@wcbpg /]# su - pgsql
Last login: Thu Dec 16 23:19:06 CST 2021 on pts/0
[pgsql@wcbpg ~]$ psql
psql (13.2)
Type "help" for help.
postgres=#