pg12.5在linux上搭建


主机配置

1.规划

数据项 内容
操作系统 redhat7.6
数据库 postgreSQL 12.5
ip地址 10.168.18.12

1.配置网卡

BOOTPROTO=none IPV6INIT=no NAME=ens34 DEVICE=ens34 IPADDR=10.168.18.12 NETMASK=255.255.255.0 USERCTL=no GATEWAY=10.168.18.1 ONBOOT=yes  

2.挂载/u01

fdisk -l vgcreate vg_pgdata /dev/sdb lvcreate -l 7679 -n lv_pgdata vg_pgdata mkfs.xfs -f /dev/vg_pgdata/lv_pgdata echo "/dev/vg_pgdata/lv_pgdata  /pgdata xfs  defaults  0  0" >>/etc/fstab mount /dev/vg_pgdata/lv_pgdata  /pgdata    

3.配置sshd

修改vi /etc/ssh/sshd_config GSSAPIAuthentication no UseDNS no systemctl restart sshd  

4.关闭防火墙与selinux

systemctl status firewalld systemctl is-enabled firewalld systemctl stop firewalld systemctl disable firewalld cp /etc/selinux/config /etc/selinux/config_`date +"%Y%m%d_%H%M%S"`&& sed -i 's/SELINUX\=enforcing/SELINUX\=disabled/g' /etc/selinux/config  

5.创建postgreSQL用户

  3.创建用户和组 # groupadd postgres /usr/sbin/useradd -m -u 501 -g postgres  -d /home/postgres -s /bin/bash -c "portgres" postgres  

6.预编译

 ./configure --prefix=/usr/local/postgresql-10.5 --with-perl --with-python     [root@pg01 postgresql-10.5]# ./configure --prefix=/usr/local/pgsql-10.5 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 WAL segment size... 16MB checking for gcc... gcc checking whether the C compiler works... yes checking for C compiler default output file name... a.out checking for suffix of executables...  checking whether we are cross compiling... no checking for suffix of object files... o checking whether we are using the GNU C compiler... yes checking whether gcc accepts -g... yes checking for gcc option to accept ISO C89... none needed checking whether gcc supports -Wdeclaration-after-statement... yes checking whether gcc supports -Wendif-labels... yes checking whether gcc supports -Wmissing-format-attribute... yes checking whether gcc supports -Wformat-security... yes checking whether gcc supports -fno-strict-aliasing... yes checking whether gcc supports -fwrapv... yes checking whether gcc supports -fexcess-precision=standard... yes checking whether gcc supports -funroll-loops... yes checking whether gcc supports -ftree-vectorize... yes checking whether gcc supports -Wunused-command-line-argument... no checking whether gcc supports -Wformat-truncation... no checking whether gcc supports -Wstringop-truncation... no checking whether the C compiler still works... yes checking how to run the C preprocessor... gcc -E checking allow thread-safe client libraries... yes checking whether to build with ICU support... no checking whether to build with Tcl... no checking whether to build Perl modules... no checking whether to build Python modules... no checking whether to build with GSSAPI support... no checking whether to build with PAM support... no checking whether to build with BSD Authentication support... no checking whether to build with LDAP support... no checking whether to build with Bonjour support... no checking whether to build with OpenSSL support... no checking whether to build with SELinux support... no checking whether to build with systemd support... no checking for grep that handles long lines and -e... /usr/bin/grep checking for egrep... /usr/bin/grep -E checking for ld used by GCC... /usr/bin/ld checking if the linker (/usr/bin/ld) is GNU ld... yes checking for ranlib... ranlib checking for strip... strip checking whether it is possible to strip libraries... yes checking for ar... ar checking for a BSD-compatible install... /usr/bin/install -c checking for tar... /usr/bin/tar checking whether ln -s works... yes checking for gawk... gawk checking for a thread-safe mkdir -p... /usr/bin/mkdir -p checking for bison... /usr/bin/bison configure: using bison (GNU Bison) 3.0.4 checking for flex... /usr/bin/flex configure: using flex 2.5.37 checking for perl... /usr/bin/perl configure: using perl 5.16.3 checking for ANSI C header files... yes checking for sys/types.h... yes checking for sys/stat.h... yes checking for stdlib.h... yes checking for string.h... yes checking for memory.h... yes checking for strings.h... yes checking for inttypes.h... yes checking for stdint.h... yes checking for unistd.h... yes checking if compiler needs certain flags to reject unknown flags... no checking for the pthreads library -lpthreads... no checking whether pthreads work without any flags... no checking whether pthreads work with -Kthread... no checking whether pthreads work with -kthread... no checking for the pthreads library -llthread... no checking whether pthreads work with -pthread... yes checking for joinable pthread attribute... PTHREAD_CREATE_JOINABLE checking if more special flags are required for pthreads... no checking for PTHREAD_PRIO_INHERIT... yes checking pthread.h usability... yes checking pthread.h presence... yes checking for pthread.h... yes checking for strerror_r... yes checking for getpwuid_r... yes checking for gethostbyname_r... yes checking whether strerror_r returns int... no checking for main in -lm... yes checking for library containing setproctitle... no checking for library containing dlopen... -ldl checking for library containing socket... none required checking for library containing shl_load... no checking for library containing getopt_long... none required checking for library containing crypt... -lcrypt checking for library containing shm_open... -lrt checking for library containing shm_unlink... none required checking for library containing clock_gettime... none required checking for library containing fdatasync... none required checking for library containing sched_yield... none required checking for library containing gethostbyname_r... none required checking for library containing shmget... none required checking for library containing readline... no configure: error: readline library not found If you have readline already installed, see config.log for details on the failure.  It is possible the compiler isn't looking in the proper directory. Use --without-readline to disable readline support.  

7.配置yum 

RHEL 7配置 mkdir -p /mnt/cdrom&&mount /dev/sr0 /mnt/cdrom&&echo "[rhel-source] name=Red Hat baseurl=file:///mnt/cdrom/ gpgcheck=0 enable=1 gpgkey=file:///mnt/cdrom/RPM-GPG-KEY-redhat-release" >/etc/yum.repos.d/rhel-source.repo     mkdir -p /mnt/cdrom&&mount /dev/sr0 /mnt/cdrom&&echo "[rhel-source] name=Red Hat baseurl=file:///mnt/cdrom/ gpgcheck=0 enable=1 gpgkey=file:////etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release" >/etc/yum.repos.d/rhel-source.repo     安装RPM包 yum install ncurses* readline* zlib* -y  

8.解析文件安装pg

1、configure,这一步一般用来生成 Makefile,为下一步的编译做准备,你可以通过在 configure 后加上参数来对安装进行控制,比如代码:./configure –prefix=/usr 意思是将该软件安装在 /usr 下面,执行文件就会安装在 /usr/bin (而不是默认的 /usr/local/bin),资源文件就会安装在 /usr/share(而不是默认的/usr/local/share)。同时一些软件的配置文件你可以通过指定 –sys-config= 参数进行设定。有一些软件还可以加上 –with、–enable、–without、–disable 等等参数对编译加以控制,你可以通过允许 ./configure –help 察看详细的说明帮助。     2、make,这一步就是编译,大多数的源代码包都经过这一步进行编译(当然有些perl或python编写的软件需要调用perl或python来进行编译)。如果 在 make 过程中出现 error ,你就要记下错误代码(注意不仅仅是最后一行),然后你可以向开发者提交 bugreport(一般在 INSTALL 里有提交地址),或者你的系统少了一些依赖库等,这些需要自己仔细研究错误代码。 3、make insatll,这条命令来进行安装(当然有些软件需要先运行 make check 或 make test 来进行一些测试),这一步一般需要你有 root 权限(因为要向系统写入文件)。   [root@pg01 data]# make --version GNU Make 3.82 Built for x86_64-redhat-linux-gnu Copyright (C) 2010  Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.   解析文件(在postgresql-10.5目录执行:make install),最后输出结果没提示异常【error】便可开始安装了 # yum install zlib-devel # make # make install  --安装 # mkdir -p /pgdata/data  创建文件目录 # chown postgres:postgres /pgdata/data # su - postgres     make的时候会出现 make[2]: Leaving directory `/pgdata/postgresql-10.5/src/test/isolation' make -C test/perl all make[2]: Entering directory `/pgdata/postgresql-10.5/src/test/perl' make[2]: Nothing to be done for `all'. make[2]: Leaving directory `/pgdata/postgresql-10.5/src/test/perl' make[1]: Leaving directory `/pgdata/postgresql-10.5/src' make -C config all make[1]: Entering directory `/pgdata/postgresql-10.5/config' make[1]: Nothing to be done for `all'. make[1]: Leaving directory `/pgdata/postgresql-10.5/config' All of PostgreSQL successfully made. Ready to install.   make install make[1]: Entering directory `/pgdata/postgresql-10.5/config' /usr/bin/mkdir -p '/usr/local/postgresql-10.5/lib/pgxs/config' /usr/bin/install -c -m 755 ./install-sh '/usr/local/postgresql-10.5/lib/pgxs/config/install-sh' /usr/bin/install -c -m 755 ./missing '/usr/local/postgresql-10.5/lib/pgxs/config/missing' make[1]: Leaving directory `/pgdata/postgresql-10.5/config' PostgreSQL installation complete.  

9.初始化数据库

su - postgres $ /usr/local/postgresql-10.5/bin/initdb -D /pgdata/data  --初始化数据库 $ /usr/local/postgresql-10.5/bin/postgres -D /pgdata/data >logfile 2>&1 &   --启动数据库   $ cd /usr/local/postgresql-10.5/bin   $ ./pg_ctl start -D /pgdata/data  --要在postgres下启动   输出日志
[root@pg01 postgresql-10.5]# su - postgres
[postgres@pg01 ~]$ /usr/local/postgresql-10.5/bin/initdb -D /pgdata/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 "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
 
Data page checksums are disabled.
 
fixing permissions on existing directory /pgdata/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
 
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:
 
    /usr/local/postgresql-10.5/bin/pg_ctl -D /pgdata/data -l logfile start

10.修改postgresql.conf

vi /pgdata/data/postgresql.conf 修改listen_addresses = '*'    

11.修改pg_hba.conf

vi /pgdata/data/pg_hba.conf host    all     all              0.0.0.0/0                 trust  

12.添加环境变量

su - postgres vi .bash_profile export PGHOME=/usr/local/postgresql-10.5 export PGDATA=/pgdata/data export PATH=$PGHOME/bin:$PATH:$HOME/bin export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/data56/lib64:/lib:/usr/lib:/data56/lib:$LD_LIBRARY_PATH  

13.重启pg

 /usr/local/postgresql-10.5/bin/pg_ctl restart -D /pgdata/data -m fast   [postgres@pg01 ~]$  /usr/local/postgresql-10.5/bin/pg_ctl restart -D /pgdata/data -m fast waiting for server to shut down.... done server stopped waiting for server to start....2021-01-25 00:26:38.152 CST [29760] LOG:  listening on IPv4 address "0.0.0.0", port 5432 2021-01-25 00:26:38.152 CST [29760] LOG:  listening on IPv6 address "::", port 5432 2021-01-25 00:26:38.152 CST [29760] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432" 2021-01-25 00:26:38.163 CST [29761] LOG:  database system was shut down at 2021-01-25 00:26:38 CST 2021-01-25 00:26:38.170 CST [29760] LOG:  database system is ready to accept connections  done server started [1]+  Done                    /usr/local/postgresql-10.5/bin/postgres -D /pgdata/data > logfile 2>&1 [postgres@pg01 ~]$  /usr/local/postgresql-10.5/bin/pg_ctl restart -D /pgdata/data -m fast waiting for server to shut down....2021-01-25 00:26:45.229 CST [29760] LOG:  received fast shutdown request 2021-01-25 00:26:45.229 CST [29760] LOG:  aborting any active transactions 2021-01-25 00:26:45.231 CST [29760] LOG:  worker process: logical replication launcher (PID 29767) exited with exit code 1 2021-01-25 00:26:45.231 CST [29762] LOG:  shutting down 2021-01-25 00:26:45.237 CST [29760] LOG:  database system is shut down  done server stopped waiting for server to start....2021-01-25 00:26:45.337 CST [29769] LOG:  listening on IPv4 address "0.0.0.0", port 5432 2021-01-25 00:26:45.337 CST [29769] LOG:  listening on IPv6 address "::", port 5432 2021-01-25 00:26:45.338 CST [29769] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432" 2021-01-25 00:26:45.351 CST [29770] LOG:  database system was shut down at 2021-01-25 00:26:45 CST 2021-01-25 00:26:45.353 CST [29769] LOG:  database system is ready to accept connections  done server started    

14.pg数据库的启停命令

[postgres@pg01 ~]$ pg_ctl stop -D /pgdata/data waiting for server to shut down....2021-01-25 00:58:47.095 CST [29769] LOG:  received fast shutdown request 2021-01-25 00:58:47.096 CST [29769] LOG:  aborting any active transactions 2021-01-25 00:58:47.097 CST [29769] LOG:  worker process: logical replication launcher (PID 29776) exited with exit code 1 2021-01-25 00:58:47.097 CST [29771] LOG:  shutting down 2021-01-25 00:58:47.103 CST [29769] LOG:  database system is shut down  done server stopped [postgres@pg01 ~]$ pg_ctl start -D /pgdata/data waiting for server to start....2021-01-25 00:59:09.422 CST [29866] LOG:  listening on IPv4 address "0.0.0.0", port 5432 2021-01-25 00:59:09.422 CST [29866] LOG:  listening on IPv6 address "::", port 5432 2021-01-25 00:59:09.423 CST [29866] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432" 2021-01-25 00:59:09.435 CST [29867] LOG:  database system was shut down at 2021-01-25 00:58:47 CST 2021-01-25 00:59:09.437 CST [29866] LOG:  database system is ready to accept connections  done server started   [postgres@pg01 ~]$ psql psql (10.5) Type "help" for help.  
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 rows)
  关闭数据库的模式有三种 pg_ctl stop -D /pgdata/data -m [mode] smart模式: 等所有的连接中止后,关闭数据库。如果有客户端连接不终止,则无法关闭数据库   fast模式 快速关闭数据库,断开客户端连接,让已经有的事务进行回滚,然后正常关闭数据库,相当于oracle关闭数据库时的immediate模式   immediate模式 立即关闭数据库,相当于数据库进程立即停止,直接退出,下次启动的时候数据库进行恢复,相当于oracle数据库关闭的时候abort模式