Oracle Linux 7.1使用源码安装Postgresql
在Oracle Linux 7.1中使用源码来安装PostgreSQL 9.6
 编译PostgreSQL需要下列软件包:
 1.GUN make版本3.80或新的要求。
 [root@cs1 /]# make --version
 GNU Make 3.82
 Built for x86_64-redhat-linux-gnu
 Copyright © 2010 Free Software Foundation, Inc.
 License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html
 This is free software: you are free to change and redistribute it.
 There is NO WARRANTY, to the extent permitted by law.
2.需要一个ISO/ANSI C 编译器(至少是 C89兼容的)。我们推荐使用最近版本的GCC
 [root@cs1 /]# gcc --version
 gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9)
 Copyright © 2013 Free Software Foundation, Inc.
 本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
 包括没有适销性和某一专用目的下的适用性担保。
3.除了gzip和bzip2之外,我们还需要tar来解包源代码发布。
4.GNU Readline库
 [root@cs1 /]# rpm -aq | grep readline
 readline-6.2-9.el7.x86_64
 [root@cs1 /]# rpm -aq | grep readline-deve
 readline-devel-6.2-9.el7.x86_64
 [root@cs1 /]# rpm -aq | grep libedit
 libedit-3.0-12.20121213cvs.el7.x86_64
5.可选工具包在默认配置的时候并不要求它们,但是如果打开了一些编译选项之后就需要它们了
 perl 5.8 or later
 python
 Kerberos
 OpenSSL
 OpenLDAP and/or PAM
 Flex 2.5.31 or later
 Bison 1.875 or later
6.创建用户与组
 [root@cs1 local]# groupadd pgsql
 [root@cs1 local]# useradd -g pgsql pgsql
7.下载源码包postgresql-9.6.6.tar.gz并上传到服务器并解压
 [root@cs1 local]# gunzip </soft/postgresql-9.6.6.tar.gz | tar vxf -
[root@cs1 local]# ls -lrt
 总用量 8
 drwxr-xr-x. 2 root root 6 5月 8 2014 src
 drwxr-xr-x. 2 root root 6 5月 8 2014 sbin
 drwxr-xr-x. 2 root root 6 5月 8 2014 libexec
 drwxr-xr-x. 2 root root 6 5月 8 2014 lib64
 drwxr-xr-x. 2 root root 6 5月 8 2014 lib
 drwxr-xr-x. 2 root root 6 5月 8 2014 include
 drwxr-xr-x. 2 root root 6 5月 8 2014 games
 drwxr-xr-x. 2 root root 6 5月 8 2014 etc
 drwxr-xr-x. 5 root root 46 10月 12 2017 share
 drwxrwxrwx 6 pgsql pgsql 4096 11月 7 2017 postgresql-9.6.6
 drwxr-xr-x. 2 root root 46 3月 9 2018 bin
 drwxrwxr-x 13 root mysql 4096 1月 31 02:40 mariadb-10.0.38-linux-glibc_214-x86_64
 lrwxrwxrwx 1 root root 38 6月 4 14:43 mysql -> mariadb-10.0.38-linux-glibc_214-x86_64
 创建一个软链接
 [root@cs1 local]# ln -s postgresql-9.6.6 pgsql
 [root@cs1 local]# ls -lrt
 总用量 8
 drwxr-xr-x. 2 root root 6 5月 8 2014 src
 drwxr-xr-x. 2 root root 6 5月 8 2014 sbin
 drwxr-xr-x. 2 root root 6 5月 8 2014 libexec
 drwxr-xr-x. 2 root root 6 5月 8 2014 lib64
 drwxr-xr-x. 2 root root 6 5月 8 2014 lib
 drwxr-xr-x. 2 root root 6 5月 8 2014 include
 drwxr-xr-x. 2 root root 6 5月 8 2014 games
 drwxr-xr-x. 2 root root 6 5月 8 2014 etc
 drwxr-xr-x. 5 root root 46 10月 12 2017 share
 drwxrwxrwx 6 pgsql pgsql 4096 11月 7 2017 postgresql-9.6.6
 drwxr-xr-x. 2 root root 46 3月 9 2018 bin
 drwxrwxr-x 13 root mysql 4096 1月 31 02:40 mariadb-10.0.38-linux-glibc_214-x86_64
 lrwxrwxrwx 1 root root 38 6月 4 14:43 mysql -> mariadb-10.0.38-linux-glibc_214-x86_64
 lrwxrwxrwx 1 root root 16 6月 4 21:24 pgsql -> postgresql-9.6.6
8.安装
 root@cs1 local]# cd pgsql
8.1安装过程的第一步就是为你的系统配置源代码树并选择你喜欢的选项。这个工作是通过运行configure脚本实现的,对于默认安装,你只需要简单地输入:
 [root@cs1 pgsql]# ./configure
 checking build system type… x86_64-pc-linux-gnu
 …省略…
 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/dynloader/linux.c to src/backend/port/dynloader.c
 config.status: linking src/backend/port/sysv_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/backend/port/dynloader/linux.h to src/include/dynloader.h
 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
默认安装目录/usr/local/pgsql,可以使用–prefix=path进行修改,./configure --help
8.2 编译
 要开始编译,键入:
 root@cs1 local]# make
 (一定要记得用GNU make)。依你的硬件而异,编译过程可能需要 5 分钟到半小时。显示的最后一行应该是:
 All of PostgreSQL successfully made. Ready to install.
 如果你希望编译所有能编译的东西,包括文档(HTML和手册页)以及附加模块(contrib),这样键入:
[root@cs1 pgsql]# make world
 …省略…
 make[2]: 离开目录“/usr/local/postgresql-9.6.6/contrib/vacuumlo”
 make[1]: 离开目录“/usr/local/postgresql-9.6.6/contrib”
 PostgreSQL, contrib, and documentation successfully made. Ready to install.
8.3安装文件
 要安装PostgreSQL,输入:
 make install
 这条命令将把文件安装到在步骤 1中指定的目录。确保你有足够的权限向该区域写入。通常你需要用 root 权限做这一步。或者你也可以事先创建目标目录并且分派合适的权限。
要安装文档(HTML和手册页),输入:
 make install-docs
 如果你按照上面的方法编译了所有东西,输入:
 make install-world
 这也会安装文档。
[root@cs1 pgsql]# make install-world
 …省略…
 make[2]: 离开目录“/usr/local/postgresql-9.6.6/contrib/vacuumlo”
 make[1]: 离开目录“/usr/local/postgresql-9.6.6/contrib”
 PostgreSQL, contrib, and documentation installation complete.
9.设置环境变量
 创建目录/usr/local/pgsql/data用来存放数据
 [root@cs1 pgsql]# mkdir /usr/local/pgsql/data
 [root@cs1 pgsql]# chown pgsql:pgsql data
[root@cs1 pgsql]# su - pgsql
 [pgsql@cs1 ~]$ vi .bash_profile
.bash_profile
Get the aliases and functions
if [ -f ~/.bashrc ]; then
 . ~/.bashrc
 fi
User specific environment and startup programs
PATH= HOME/.local/bin:$HOME/bin
export PATH
 export LD_LIBRARY_PATH=/usr/local/pgsql/lib
 export PG_HOME=/usr/local/pgsql
 export PATH=
    PATH
 export PGDATA=/usr/local/pgsql/data
10.初始化数据库
 使用pgsql用户来执行
 [pgsql@cs1 ~]$ initdb --help
 initdb initializes a PostgreSQL database cluster.
Usage:
 initdb [OPTION]… [DATADIR]
Options:
 -A, --auth=METHOD default authentication method for local connections
 –auth-host=METHOD default authentication method for local TCP/IP connections
 –auth-local=METHOD default authentication method for local-socket connections
 [-D, --pgdata=]DATADIR location for this database cluster
 -E, --encoding=ENCODING set default encoding for new databases
 –locale=LOCALE set default locale for new databases
 –lc-collate=, --lc-ctype=, --lc-messages=LOCALE
 –lc-monetary=, --lc-numeric=, --lc-time=LOCALE
 set default locale in the respective category for
 new databases (default taken from environment)
 –no-locale equivalent to --locale=C
 –pwfile=FILE read password for the new superuser from file
 -T, --text-search-config=CFG
 default text search configuration
 -U, --username=NAME database superuser name
 -W, --pwprompt prompt for a password for the new superuser
 -X, --xlogdir=XLOGDIR location for the transaction log directory
Less commonly used options:
 -d, --debug generate lots of debugging output
 -k, --data-checksums use data page checksums
 -L DIRECTORY where to find the input files
 -n, --noclean do not clean up after errors
 -N, --nosync do not wait for changes to be written safely to disk
 -s, --show show internal settings
 -S, --sync-only only sync data directory
Other options:
 -V, --version output version information, then exit
 -?, --help show this help, then exit
If the data directory is not specified, the environment variable PGDATA
 is used.
Report bugs to pgsql-bugs@postgresql.org.
[pgsql@cs1 ~]$ initdb -D /usr/local/pgsql/data
 The files belonging to this database system will be owned by user “pgsql”.
 This user must also own the server process.
The database cluster will be initialized with locale “zh_CN.gb2312”.
 The default database encoding has accordingly been set to “EUC_CN”.
 initdb: could not find suitable text search configuration for locale “zh_CN.gb2312”
 The default text search configuration will be set to “simple”.
Data page checksums are disabled.
fixing permissions on existing directory /usr/local/pgsql/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:
pg_ctl -D /usr/local/pgsql/data -l logfile start
同时在pgsql的目录可以看到生成的数据目录data以及该目录的相关数据和配置文件:
 [root@cs1 soft]# cd /usr/local/pgsql
 [root@cs1 pgsql]# ls -lrt
 总用量 1080
 -rw-r–r-- 1 pgsql pgsql 1212 11月 7 2017 README
 -rw-r–r-- 1 pgsql pgsql 1529 11月 7 2017 Makefile
 -rw-r–r-- 1 pgsql pgsql 284 11月 7 2017 HISTORY
 -rw-r–r-- 1 pgsql pgsql 3638 11月 7 2017 GNUmakefile.in
 -rw-r–r-- 1 pgsql pgsql 1192 11月 7 2017 COPYRIGHT
 -rw-r–r-- 1 pgsql pgsql 74867 11月 7 2017 configure.in
 -rwxr-xr-x 1 pgsql pgsql 471555 11月 7 2017 configure
 -rw-r–r-- 1 pgsql pgsql 384 11月 7 2017 aclocal.m4
 drwxrwxrwx 55 pgsql pgsql 4096 11月 7 2017 contrib
 drwxrwxrwx 2 pgsql pgsql 4096 11月 7 2017 config
 drwxrwxrwx 3 pgsql pgsql 101 11月 7 2017 doc
 -rw-r–r-- 1 pgsql pgsql 76016 11月 7 2017 INSTALL
 -rwxr-xr-x 1 root root 39264 6月 4 21:29 config.status
 -rw-r–r-- 1 root root 3638 6月 4 21:29 GNUmakefile
 drwxrwxrwx 16 pgsql pgsql 4096 6月 4 21:29 src
 -rw-r–r-- 1 root root 370213 6月 4 21:29 config.log
 drwxr-xr-x 4 root root 26 6月 4 21:43 tmp_install
 drwxr-xr-x 6 root root 4096 6月 4 21:45 include
 drwxr-xr-x 8 root root 4096 6月 4 21:45 share
 drwxr-xr-x 4 root root 4096 6月 4 21:45 lib
 drwxr-xr-x 2 root root 4096 6月 4 21:45 bin
 drwx------ 19 pgsql pgsql 4096 6月 4 21:56 data
 [root@cs1 pgsql]# cd data
 [root@cs1 data]# ls -lrt
 总用量 48
 drwx------ 2 pgsql pgsql 6 6月 4 21:56 pg_twophase
 drwx------ 2 pgsql pgsql 6 6月 4 21:56 pg_snapshots
 drwx------ 2 pgsql pgsql 6 6月 4 21:56 pg_serial
 drwx------ 2 pgsql pgsql 6 6月 4 21:56 pg_replslot
 drwx------ 4 pgsql pgsql 34 6月 4 21:56 pg_multixact
 drwx------ 2 pgsql pgsql 6 6月 4 21:56 pg_dynshmem
 drwx------ 2 pgsql pgsql 6 6月 4 21:56 pg_commit_ts
 -rw------- 1 pgsql pgsql 4 6月 4 21:56 PG_VERSION
 drwx------ 2 pgsql pgsql 6 6月 4 21:56 pg_tblspc
 drwx------ 2 pgsql pgsql 6 6月 4 21:56 pg_stat_tmp
 drwx------ 2 pgsql pgsql 6 6月 4 21:56 pg_stat
 drwx------ 4 pgsql pgsql 37 6月 4 21:56 pg_logical
 -rw------- 1 pgsql pgsql 22304 6月 4 21:56 postgresql.conf
 -rw------- 1 pgsql pgsql 88 6月 4 21:56 postgresql.auto.conf
 -rw------- 1 pgsql pgsql 1636 6月 4 21:56 pg_ident.conf
 -rw------- 1 pgsql pgsql 4459 6月 4 21:56 pg_hba.conf
 drwx------ 3 pgsql pgsql 58 6月 4 21:56 pg_xlog
 drwx------ 2 pgsql pgsql 17 6月 4 21:56 pg_subtrans
 drwx------ 2 pgsql pgsql 17 6月 4 21:56 pg_clog
 drwx------ 2 pgsql pgsql 17 6月 4 21:56 pg_notify
 drwx------ 2 pgsql pgsql 4096 6月 4 21:56 global
 drwx------ 5 pgsql pgsql 38 6月 4 21:56 base
11.启动数据库
 在初始化数据库结束后看到了启动命令:
 Success. You can now start the database server using:
pg_ctl -D /usr/local/pgsql/data -l logfile start
[pgsql@cs1 ~]$ echo $PGDATA
 /usr/local/pgsql/data
 由于我们设置了环境变量,所以已经指定了数据目录PGDATA, -l表示日志文件目录,通常需要指定,所以我们在
 /usr/local/pgsql根目录下再创建一个log目录用来存放日志文件(注意别忘记赋予可写的权限)
 [root@cs1 pgsql]# chown -R pgsql:pgsql log
 [root@cs1 pgsql]# chmod -R 775 log
 [root@cs1 pgsql]# ls -lrt
 总用量 1080
 -rw-r–r-- 1 pgsql pgsql 1212 11月 7 2017 README
 -rw-r–r-- 1 pgsql pgsql 1529 11月 7 2017 Makefile
 -rw-r–r-- 1 pgsql pgsql 284 11月 7 2017 HISTORY
 -rw-r–r-- 1 pgsql pgsql 3638 11月 7 2017 GNUmakefile.in
 -rw-r–r-- 1 pgsql pgsql 1192 11月 7 2017 COPYRIGHT
 -rw-r–r-- 1 pgsql pgsql 74867 11月 7 2017 configure.in
 -rwxr-xr-x 1 pgsql pgsql 471555 11月 7 2017 configure
 -rw-r–r-- 1 pgsql pgsql 384 11月 7 2017 aclocal.m4
 drwxrwxrwx 55 pgsql pgsql 4096 11月 7 2017 contrib
 drwxrwxrwx 2 pgsql pgsql 4096 11月 7 2017 config
 drwxrwxrwx 3 pgsql pgsql 101 11月 7 2017 doc
 -rw-r–r-- 1 pgsql pgsql 76016 11月 7 2017 INSTALL
 -rwxr-xr-x 1 root root 39264 6月 4 21:29 config.status
 -rw-r–r-- 1 root root 3638 6月 4 21:29 GNUmakefile
 drwxrwxrwx 16 pgsql pgsql 4096 6月 4 21:29 src
 -rw-r–r-- 1 root root 370213 6月 4 21:29 config.log
 drwxr-xr-x 4 root root 26 6月 4 21:43 tmp_install
 drwxr-xr-x 6 root root 4096 6月 4 21:45 include
 drwxr-xr-x 8 root root 4096 6月 4 21:45 share
 drwxr-xr-x 4 root root 4096 6月 4 21:45 lib
 drwxr-xr-x 2 root root 4096 6月 4 21:45 bin
 drwx------ 19 pgsql pgsql 4096 6月 4 21:56 data
 drwxrwxr-x 2 pgsql pgsql 6 6月 4 22:04 log
运行pg_ctl start -l /usr/local/pgsql/log/pg_server.log即可启动数据库
[pgsql@cs1 ~]$ pg_ctl start -l /usr/local/pgsql/log/pg_server.log
 server starting
通过ps -ef|grep postgres查看一下postgres相关是否存在相关进程
 [root@cs1 log]# ps -ef|grep postgres
 pgsql 4977 1 0 22:05 pts/3 00:00:00 /usr/local/postgresql-9.6.6/bin/postgres
 pgsql 4980 4977 0 22:05 ? 00:00:00 postgres: checkpointer process
 pgsql 4981 4977 0 22:05 ? 00:00:00 postgres: writer process
 pgsql 4982 4977 0 22:05 ? 00:00:00 postgres: wal writer process
 pgsql 4983 4977 0 22:05 ? 00:00:00 postgres: autovacuum launcher process
 pgsql 4984 4977 0 22:05 ? 00:00:00 postgres: stats collector process
 root 5145 15622 0 22:06 pts/4 00:00:00 grep --color=auto postgres
12.连接数据库
 [pgsql@cs1 ~]$ psql --list
 List of databases
 Name | Owner | Encoding | Collate | Ctype | Access privileges
 -----------±------±---------±-------------±-------------±------------------
 postgres | pgsql | EUC_CN | zh_CN.gb2312 | zh_CN.gb2312 |
 template0 | pgsql | EUC_CN | zh_CN.gb2312 | zh_CN.gb2312 | =c/pgsql +
 | | | | | pgsql=CTc/pgsql
 template1 | pgsql | EUC_CN | zh_CN.gb2312 | zh_CN.gb2312 | =c/pgsql +
 | | | | | pgsql=CTc/pgsql
 (3 rows)
启动成功后我们就可以通过postgresql自带的客户端工具psql来进行连接,直接输入psql看到版本信息则说明连接成功:
 [pgsql@cs1 ~]$ psql postgres
 psql (9.6.6)
 Type “help” for help.
postgres=#
接下来要做的第一件事就是设置postgres用户的密码(默认为空),用psql连接成功后直接输入\password即会提示输入两次密码
[pgsql@cs1 ~]$ psql postgres
 psql (9.6.6)
 Type “help” for help.
postgres=# \password
 Enter new password:
 Enter it again:
 postgres=# \l
 List of databases
 Name | Owner | Encoding | Collate | Ctype | Access privileges
 -----------±------±---------±-------------±-------------±------------------
 postgres | pgsql | EUC_CN | zh_CN.gb2312 | zh_CN.gb2312 |
 template0 | pgsql | EUC_CN | zh_CN.gb2312 | zh_CN.gb2312 | =c/pgsql +
 | | | | | pgsql=CTc/pgsql
 template1 | pgsql | EUC_CN | zh_CN.gb2312 | zh_CN.gb2312 | =c/pgsql +
 | | | | | pgsql=CTc/pgsql
 (3 rows)
postgres=# \l
 List of databases
 Name | Owner | Encoding | Collate | Ctype | Access privileges
 -----------±------±---------±-------------±-------------±------------------
 postgres | pgsql | EUC_CN | zh_CN.gb2312 | zh_CN.gb2312 |
 template0 | pgsql | EUC_CN | zh_CN.gb2312 | zh_CN.gb2312 | =c/pgsql +
 | | | | | pgsql=CTc/pgsql
 template1 | pgsql | EUC_CN | zh_CN.gb2312 | zh_CN.gb2312 | =c/pgsql +
 | | | | | pgsql=CTc/pgsql
 (3 rows)
 postgres=# select version();
 version
 
 PostgreSQL 9.6.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9), 64-bit
 (1 row)
 postgres=# select current_date;
 date
 
 2019-06-04
 (1 row)
postgres=#
到此使用源码来安装pg数据库就完成了。
- 点赞
- 收藏
- 关注作者
 
             
           
评论(0)