部署服务器时,数据库服务器是在局域网环境下,不对外公开访问,虽然能够通过ssh操作但是在安装应用软件时基本的yum安装并不能达到目的。也许可以下载rpm进行yum,但也许会死于依赖.
案例环境
- 一台可以公网访问的阿里云ECS -- A
- 一台无公网访问的阿里云ECS -- B
下载postgresql源码并解压
在A上下载
1 [root@iZuf------------kZ resource]# wget https://ftp.postgresql.org/pub/source/v9.6.6/postgresql-9.6.6.tar.gz 2 --2017-12-05 13:24:04-- https://ftp.postgresql.org/pub/source/v9.6.6/postgresql-9.6.6.tar.gz 3 Resolving ftp.postgresql.org (ftp.postgresql.org)… 217.196.149.55, 174.143.35.246, 87.238.57.227, … 4 Connecting to ftp.postgresql.org (ftp.postgresql.org)|217.196.149.55|:443… connected. 5 HTTP request sent, awaiting response… 200 OK 6 Length: 25709021 (25M) [application/x-gzip] 7 Saving to: ‘postgresql-9.6.6.tar.gz’ 8 9 100%[=============================================================================================================================================>] 25,709,021 4.31MB/s in 6.3s 10 11 2017-12-05 13:24:13 (3.92 MB/s) - ‘postgresql-9.6.6.tar.gz’ saved [25709021/25709021] 12 13 [root@iZuf------------kZ resource]# ls 14 postgresql-9.6.6.tar.gz 15 [root@iZuf------------kZ resource]# tar zxvf postgresql-9.6.6.tar.gz 16 [root@iZuf------------kZ resource]# ls 17 postgresql-9.6.6 postgresql-9.6.6.tar.gz
- 将下载文件发送到B上
1 [root@iZuf------------kZ resource]# scp postgresql-9.6.6.tar.gz root@ip地址:路径
新增postgres用户及用户组
1 [root@iZuf------------kZ resource]# groupadd postgres 2 [root@iZuf------------kZ resource]# useradd postgres -g postgres
一般需要环境如下,可提前准备,也可以根据编译提示信息补安装
1 [root@iZuf------------kZ postgresql-9.6.6]# yum -y install wget gcc readline-devel zlib-devel make
编译
1 [root@iZuf------------kZ postgresql-9.6.6]# ls 2 aclocal.m4 config configure configure.in contrib COPYRIGHT doc GNUmakefile.in HISTORY INSTALL Makefile README src 3 [root@iZuf------------kZ postgresql-9.6.6]# ./configure --prefix=/usr/local/pgsql
如果出现编译错误则需关注报错信息 ,如下 缺少 readline-devel zlib
1 configure: error: readline library not found 2 If you have readline already installed, see config.log for details on the 3 failure. It is possible the compiler isn't looking in the proper directory. 4 Use --without-readline to disable readline support. 5 [root@iZuf------------kZ postgresql-9.6.6]# 6 … 7 configure: error: zlib library not found 8 If you have zlib already installed, see config.log for details on the 9 failure. It is possible the compiler isn't looking in the proper directory. 10 Use --without-zlib to disable zlib support. 11 [root@iZuf------------kZ postgresql-9.6.6]# 12 [root@iZuf------------kZ postgresql-9.6.6]# yum -y install gcc gcc-c++ readline-devel zlib-devel make
如下提示为本次编译成功
1 configure: using CPPFLAGS= -D_GNU_SOURCE 2 configure: using LDFLAGS= -Wl,--as-needed 3 configure: creating ./config.status 4 config.status: creating GNUmakefile 5 config.status: creating src/Makefile.global 6 config.status: creating src/include/pg_config.h 7 config.status: creating src/include/pg_config_ext.h 8 config.status: creating src/interfaces/ecpg/include/ecpg_config.h 9 config.status: linking src/backend/port/tas/dummy.s to src/backend/port/tas.s 10 config.status: linking src/backend/port/dynloader/linux.c to src/backend/port/dynloader.c 11 config.status: linking src/backend/port/sysv_sema.c to src/backend/port/pg_sema.c 12 config.status: linking src/backend/port/sysv_shmem.c to src/backend/port/pg_shmem.c 13 config.status: linking src/backend/port/dynloader/linux.h to src/include/dynloader.h 14 config.status: linking src/include/port/linux.h to src/include/pg_config_os.h 15 config.status: linking src/makefiles/Makefile.linux to src/Makefile.port 16 [root@iZuf------------kZ postgresql-9.6.6]# ls
安装
- make && make install
[root@iZuf------------kZ postgresql-9.6.6]# make && make install
最终提示PostgreSQL installation complete.表示安装成功
- 扩展安装 contrib make && make install
1 [root@iZuf------------kZ postgresql-9.6.6]# ls 2 aclocal.m4 config config.log config.status configure configure.in contrib COPYRIGHT doc GNUmakefile GNUmakefile.in HISTORY INSTALL Makefile README src 3 [root@iZuf------------kZ postgresql-9.6.6]# cd contrib/ 4 [root@iZuf------------kZ contrib]# ls 5 adminpack btree_gist dblink fuzzystrmatch intarray Makefile pgcrypto pg_stat_statements README start-scripts tsm_system_rows xml2 6 auth_delay chkpass dict_int hstore isn oid2name pg_freespacemap pgstattuple seg tablefunc tsm_system_time 7 auto_explain citext dict_xsyn hstore_plperl lo pageinspect pg_prewarm pg_trgm sepgsql tcn unaccent 8 bloom contrib-global.mk earthdistance hstore_plpython ltree passwordcheck pgrowlocks pg_visibility spi test_decoding uuid-ossp 9 btree_gin cube file_fdw intagg ltree_plpython pg_buffercache pg_standby postgres_fdw sslinfo tsearch2 vacuumlo 10 [root@iZuf------------kZ contrib]# make && make install
配置权限路径
- 数据库路径
1 [root@iZuf------------kZ ~]# chown -R postgres:postgres /usr/local/psql
- 数据data目录
1 [root@iZuf------------kZ ~]# mkdir -p /usr/local/psql/data 2 [root@iZuf------------kZ ~]# chown postgres:postgres /usr/local/psql/data
初始化数据库
- initdb
1 [postgres@iZuf------------kZ bin]$ ./initdb --encoding=UTF-8 -D /usr/local/pgsql/data/ 2 The files belonging to this database system will be owned by user "postgres". 3 This user must also own the server process. 4 5 The database cluster will be initialized with locale "en_US.UTF-8". 6 The default text search configuration will be set to "english". 7 8 Data page checksums are disabled. 9 10 fixing permissions on existing directory /usr/local/pgsql/data … ok 11 creating subdirectories … ok 12 selecting default max_connections … 100 13 selecting default shared_buffers … 128MB 14 selecting dynamic shared memory implementation … posix 15 creating configuration files … ok 16 running bootstrap script … ok 17 performing post-bootstrap initialization … ok 18 syncing data to disk … ok 19 20 WARNING: enabling "trust" authentication for local connections 21 You can change this by editing pg_hba.conf or using the option -A, or 22 --auth-local and --auth-host, the next time you run initdb. 23 24 Success. You can now start the database server using: 25 26 ./pg_ctl -D /usr/local/pgsql/data/ -l logfile start 27 28 [postgres@iZuf------------kZ bin]$
- 配置环境变量 在/etc/profile追加配置,注意注意退出postgres用户
1 [postgres@iZuf------------kZ bin]$ exit 2 exit 3 [root@iZuf------------kZ pgsql]# vim /etc/profile 4 [root@iZuf------------kZ pgsql]# tail -n 2 /etc/profile 5 #postresql-9.6.6 6 export PATH=$PATH:/usr/local/pgsql/bin 7 [root@iZuf------------kZ pgsql]# source /etc/profile 8 [root@iZuf------------kZ pgsql]#
启动数据库
1 [postgres@iZuf------------kZ bin]$ ./pg_ctl -D /usr/local/pgsql/data/ -l logfile start 2 server starting 3 [postgres@iZuf------------kZ bin]$ psql 4 psql (9.6.6) 5 Type "help" for help. 6 7 postgres=# \l 8 List of databases 9 Name | Owner | Encoding | Collate | Ctype | Access privileges 10 -----------+----------+----------+-------------+-------------+----------------------- 11 postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | 12 template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + 13 | | | | | postgres=CTc/postgres 14 template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres + 15 | | | | | postgres=CTc/postgres 16 (3 rows)
执行psql报错,这个漏掉了配置环境变量或环境变量没有生效
1 [postgres@iZuf------------kZ bin]$ psql 2 bash: psql: command not found
至此 postgresql安装完成。