环境准备:
关闭防火墙:
systemctl stop firewalld 和vi /etc/selinux/config 中修改selinux设置为disabled 或setenforce 0
或者systemctl disable firewalld
时间同步,让系统时间等于当前时间:
查看当前时间:date
删除/etc/localtime文件,并复制shanghai时区文件替换到这里:
rm -rf /etc/localtime
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
修改虚拟机时间与当前时间同步:yum install ntpdate -y
ntpdate ntp.api.bz
/usr/sbin/ntpdate time.nist.gov
输入命令每次开启虚拟机时间与当前时间同步:hwclock -w
重启:shutdown -r now
1、创建文件夹存放pg压缩包,并解压压缩包:
mkdir /pgsoft
tar -xvf /pgsoft/postgresql-12.0.tar.gz -C /pgsoft/
2、 进入解压后生成的目录文件进行编译(./configure是用来检测b安装平台的目标特性的,一般用来生成Makefile,为下一步编译做准备。./configure -help查看说明帮助)
cd /pgsoft/postgresql-12.0
./configure --prefix=/pgsoft/pgsql --without-readline
[root@localhost ~]# cd /pgsoft/postgresql-12.0
[root@localhost postgresql-12.0]# ./configure --prefix=/pgsoft/pgsql --without-readline
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking which template to use... linux
checking whether NLS is wanted... no
checking for default port number... 5432
checking for block size... 8kB
checking for segment size... 1GB
checking for WAL block size... 8kB
checking for gcc... no
checking for cc... no
configure: error: in `/pgsoft/postgresql-12.0':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
编译报错缺少安装包,执行下面命令,yum安装:
yum install gcc
yum install ncurses* readline* zlib* -y
之后再次编译:
[root@localhost postgresql-12.0]# ./configure --prefix=/pgsoft/pgsql --without-readline
..........
..........
checking which CRC-32C implementation to use... SSE 4.2 with runtime check
checking for library containing sem_init... -lpthread
checking which semaphore API to use... unnamed POSIX
checking for /dev/urandom... yes
checking which random number source to use... /dev/urandom
checking for xmllint... /usr/bin/xmllint
checking for DocBook XML V4.2... yes
checking for dbtoepub... no
checking for xsltproc... no
checking for fop... no
checking thread safety of required library functions... yes
checking whether gcc -std=gnu99 supports -Wl,--as-needed... yes
configure: using compiler=gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
configure: using CFLAGS=-Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -O2
configure: using CPPFLAGS= -D_GNU_SOURCE
configure: using LDFLAGS= -Wl,--as-needed
configure: creating ./config.status
config.status: creating GNUmakefile
config.status: creating src/Makefile.global
config.status: creating src/include/pg_config.h
config.status: creating src/include/pg_config_ext.h
config.status: creating src/interfaces/ecpg/include/ecpg_config.h
config.status: linking src/backend/port/tas/dummy.s to src/backend/port/tas.s
config.status: linking src/backend/port/posix_sema.c to src/backend/port/pg_sema.c
config.status: linking src/backend/port/sysv_shmem.c to src/backend/port/pg_shmem.c
config.status: linking src/include/port/linux.h to src/include/pg_config_os.h
config.status: linking src/makefiles/Makefile.linux to src/Makefile.port
3、编译(时间较长,一直等着即可,出现All of PostgreSQL successfully made. Ready to install.即表示成功)
make (编译,从Makefile中读取指令,然后编译)
[root@localhost postgresql-12.0]#make
...............
...............
make[2]: 离开目录“/pgsoft/postgresql-12.0/src/test/isolation”
make -C test/perl all
make[2]: 进入目录“/pgsoft/postgresql-12.0/src/test/perl”
make[2]: 对“all”无需做任何事。
make[2]: 离开目录“/pgsoft/postgresql-12.0/src/test/perl”
make[1]: 离开目录“/pgsoft/postgresql-12.0/src”
make -C config all
make[1]: 进入目录“/pgsoft/postgresql-12.0/config”
make[1]: 对“all”无需做任何事。
make[1]: 离开目录“/pgsoft/postgresql-12.0/config”
All of PostgreSQL successfully made. Ready to install.
4、安装(时间较长,一直等着即可,出现PostgreSQL installation complete.即表示成功)
make install (安装,从Makefile中读取指令,安装到指定位置)
[root@localhost postgresql-12.0]# make install
............
............
make[2]: 离开目录“/pgsoft/postgresql-12.0/src/test/perl”
/usr/bin/mkdir -p '/pgsoft/pgsql/lib/pgxs/src'
/usr/bin/install -c -m 644 Makefile.global '/pgsoft/pgsql/lib/pgxs/src/Makefile.global'
/usr/bin/install -c -m 644 Makefile.port '/pgsoft/pgsql/lib/pgxs/src/Makefile.port'
/usr/bin/install -c -m 644 ./Makefile.shlib '/pgsoft/pgsql/lib/pgxs/src/Makefile.shlib'
/usr/bin/install -c -m 644 ./nls-global.mk '/pgsoft/pgsql/lib/pgxs/src/nls-global.mk'
make[1]: 离开目录“/pgsoft/postgresql-12.0/src”
make -C config install
make[1]: 进入目录“/pgsoft/postgresql-12.0/config”
/usr/bin/mkdir -p '/pgsoft/pgsql/lib/pgxs/config'
/usr/bin/install -c -m 755 ./install-sh '/pgsoft/pgsql/lib/pgxs/config/install-sh'
/usr/bin/install -c -m 755 ./missing '/pgsoft/pgsql/lib/pgxs/config/missing'
make[1]: 离开目录“/pgsoft/postgresql-12.0/config”
PostgreSQL installation complete.
5、编译成功的标志:
在/pgsoft文件夹下生成pgsql文件
6、 创建postgres用户,设置/pgsoft/目录文件属主组:
useradd postgres
chown -R postgres:postgres /pgsoft/
7、创建数据库库文件存储目录、给postgres赋予权限:
mkdir /pgsoft/pgsql/data
chown -R postgres:postgres /pgsoft/*
8、进入postgres用户,初始化数据库:
su postgres
/pgsoft/pgsql/bin/initdb -D /pgsoft/pgsql/data/
[root@localhost pgsoft]# su postgres
[postgres@localhost pgsoft]$ /pgsoft/pgsql/bin/initdb -D /pgsoft/pgsql/data/
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale "zh_CN.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"
The default text search configuration will be set to "simple".
Data page checksums are disabled.
fixing permissions on existing directory /pgsoft/pgsql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... PRC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
/pgsoft/pgsql/bin/pg_ctl -D /pgsoft/pgsql/data/ -l logfile start
9 、启动数据库服务:
/pgsoft/pgsql/bin/pg_ctl start -D /pgsoft/pgsql/data/
[postgres@localhost pgsoft]$ /pgsoft/pgsql/bin/pg_ctl start -D /pgsoft/pgsql/data/
waiting for server to start....2023-11-03 13:54:22.677 CST [8981] LOG: starting PostgreSQL 12.0 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
2023-11-03 13:54:22.678 CST [8981] LOG: listening on IPv6 address "::1", port 5432
2023-11-03 13:54:22.678 CST [8981] LOG: listening on IPv4 address "127.0.0.1", port 5432
2023-11-03 13:54:22.679 CST [8981] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2023-11-03 13:54:22.693 CST [8982] LOG: database system was shut down at 2023-11-03 13:54:02 CST
2023-11-03 13:54:22.696 CST [8981] LOG: database system is ready to accept connections
done
server started
10、进入postgres数据库,修改初始密码:
/pgsoft/pgsql/bin/psql
alter user postgres with password 'postgres';
[postgres@localhost pgsoft]$ /pgsoft/pgsql/bin/psql
psql (12.0)
Type "help" for help.
postgres=# alter user postgres with password 'postgres';
ALTER ROLE
postgres-#
11、修改配置文件postgresql.conf(直接文件中添加脚本即可):
vi /pgsoft/pgsql/data/postgresql.conf
listen_addresses = '*'
port = 5432
#启用pg_log并配置日志参数
log_destination = 'csvlog'
logging_collector = on # 启动日志收集, 这是一个后台进程,抓取发送到stderr的日志消息,并会将他们重定向到日志文件。
log_directory = 'pg_log'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
log_rotation_age = 1d ##日志保留天数,这里看实际环境,如果是测试建议1d,如果是生产环境建议7d
log_rotation_size = 100MB
log_min_messages = info
# 记录执行慢的SQL
log_min_duration_statement = 1000 #单位ms
log_checkpoints = on
log_connections = on
log_disconnections = on
log_duration = on
log_line_prefix = '%m'
# 监控数据库中长时间的锁
log_lock_waits = on
# 记录DDL操作
log_statement = 'all'
12 、修改配置文件pg_hba.conf:
trust改成password
13、编辑操作系统数据库管理员的用户配置文件
vi ~/.bash_profile
文件中添加
export PG_DATA=/pgsoft/pgsql/data
export PG_HOME=/pgsoft/pgsql/bin # 宕机,自启动
PATH=$PATH:$HOME/bin:$PG_HOME:$PG_DATA
export PATH
重新加载任务配置信息:
source ~/.bash_profile
14、进入postgres用户,重启数据库:
su postgres
/pgsoft/pgsql/bin/pg_ctl restart -D /pgsoft/pgsql/data/