01-postgres9.4.5在redhat6.5环境下源码编译安装(2016-11-08)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29622761/article/details/53088041

0、读者须知:

1、安装前请阅读笔记末尾的三篇安装文档,还算靠谱。
2、跟着这篇笔记安装肯定是可以安装好的,不用担心。
3、我用的是redhat6.5,更改yum源很关键。末尾的三篇安装文档都是安装在centos上的。
4、安装依赖包很关键。
5、postgresql-9.4.5安装包下载地址是:http://ftp.postgresql.org/pub/source/v9.4.5/postgresql-9.4.5.tar.bz2
可以下载在本地后上传至linux,也可以用命令下载:wget http://ftp.postgresql.org/pub/source/v9.4.5/postgresql-9.4.5.tar.bz2


1、安装redhat6.5

步骤省略。

2、更改本地yum源

[root@pgserver01 media]# cd /etc/yum.repos.d/
[root@pgserver01 yum.repos.d]# ls
packagekit-media.repo  rhel-source.repo
[root@pgserver01 yum.repos.d]#

把这两个文件删除,然后重新建立一个文件/etc/yum.repos.d/local.repo

[root@pgserver01 etc]# rm -f yum.repos.d/*
[root@pgserver01 yum.repos.d]# vi local.repo
[root@pgserver01 yum.repos.d]# cat local.repo
[Local]
name=Local
baseurl=file:///mnt/media
enable=1
gpgcheck=0
[root@pgserver01 yum.repos.d]# mkdir /mnt/media
[root@pgserver01 yum.repos.d]# mount /dev/cdrom /mnt/media/
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@pgserver01 yum.repos.d]# yum

4、解压postgres

[root@pgserver01 ~]# cd /opt/
[root@pgserver01 opt]# ll
total 4
drwxr-xr-x. 2 root root 4096 May 17  2013 rh
[root@pgserver01 opt]# mkdir soft
[root@pgserver01 opt]# ll
total 8
drwxr-xr-x. 2 root root 4096 May 17  2013 rh
drwxr-xr-x. 2 root root 4096 Nov  9 02:18 soft
[root@pgserver01 opt]# cd soft/
[root@pgserver01 soft]# ll
total 17248
-rw-r--r--. 1 root root 17660960 Nov  9 02:38 postgresql-9.4.5.tar.bz2
[root@pgserver01 soft]# chmod +x postgresql-9.4.5.tar.bz2
[root@pgserver01 soft]# ll
total 17248
-rwxr-xr-x. 1 root root 17660960 Nov  9 02:38 postgresql-9.4.5.tar.bz2
[root@pgserver01 soft]#
解压
[root@pgserver01 soft]# tar -xvf postgresql-9.4.5.tar.bz2
[root@pgserver01 soft]# ll
total 17252
drwxrwxrwx. 6 1107 1107     4096 Oct  6  2015 postgresql-9.4.5
-rwxr-xr-x. 1 root root 17660960 Nov  9  2016 postgresql-9.4.5.tar.bz2
[root@pgserver01 soft]#
[root@pgserver01 soft]# mv postgresql-9.4.5 /usr/local/
[root@pgserver01 soft]# cd /usr/local/
[root@pgserver01 local]# ll
total 44
drwxr-xr-x. 2 root root 4096 Jun 28  2011 bin
drwxr-xr-x. 2 root root 4096 Jun 28  2011 etc
drwxr-xr-x. 2 root root 4096 Jun 28  2011 games
drwxr-xr-x. 2 root root 4096 Jun 28  2011 include
drwxr-xr-x. 2 root root 4096 Jun 28  2011 lib
drwxr-xr-x. 2 root root 4096 Jun 28  2011 lib64
drwxr-xr-x. 2 root root 4096 Jun 28  2011 libexec
drwxrwxrwx. 6 1107 1107 4096 Oct  6  2015 postgresql-9.4.5
drwxr-xr-x. 2 root root 4096 Jun 28  2011 sbin
drwxr-xr-x. 5 root root 4096 Nov  9  2016 share
drwxr-xr-x. 2 root root 4096 Jun 28  2011 src
[root@pgserver01 local]# cd postgresql-9.4.5/

5、安装依赖包

[root@pgserver01 postgresql-9.4.5]# yum install gcc*
[root@pgserver01 postgresql-9.4.5]# yum -y install readline-devel

依赖包不安转,接下来就会出错。

6、增加用户设置密码

[root@pgserver01 postgresql-9.4.5]# adduser postgres
[root@pgserver01 postgresql-9.4.5]# passwd postgres
Changing password for user postgres.
New password:
BAD PASSWORD: it is based on a dictionary word
Retype new password:
passwd: all authentication tokens updated successfully.
[root@pgserver01 postgresql-9.4.5]#

密码是:postgres

7、开始编译和安装postgres

[root@pgserver01 postgresql-9.4.5]# ./configure --prefix=/home/postgres/pgsql
[root@pgserver01 postgresql-9.4.5]# gmake 
[root@pgserver01 postgresql-9.4.5]# gmake install

如果这里出现问题,一般是因为依赖包没有安装好。

8、设置环境变量

[root@pgserver01 postgres]# cd /home/postgres/
[root@pgserver01 postgres]# vim .bash_profile
[root@pgserver01 postgres]#

这里写图片描述

[root@pgserver01 postgres]# source .bash_profile

9、初始化数据库

新建数据目录

[root@pgserver01 postgres]# mkdir /home/postgres/pgsql/data

更改所属组和用户

[root@pgserver01 postgres]# chown postgres:postgres /home/postgres/pgsql/data
[root@pgserver01 postgres]# su - postgres
mount: only root can do that

这个“mount mount: only root can do that ”是我配置yum源的操作,不用理会。
开始初始化:

[postgres@pgserver01 ~]$ /home/postgres/pgsql/bin/initdb -D /home/postgres/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 "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 /home/postgres/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
creating template1 database in /home/postgres/pgsql/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... 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:
    /home/postgres/pgsql/bin/postgres -D /home/postgres/pgsql/data
or
    /home/postgres/pgsql/bin/pg_ctl -D /home/postgres/pgsql/data -l logfile start
[postgres@pgserver01 ~]$

10、系统服务

回到root用户, 复制安装目录下的linux文件到/etc/init.d/

[root@pgserver01 ~]# cd /usr/local/postgresql-9.4.5/
[root@pgserver01 postgresql-9.4.5]# cp contrib/start-scripts/linux /etc/init.d/postgresql
[root@pgserver01 postgresql-9.4.5]#
[root@pgserver01 postgresql-9.4.5]# vim /etc/init.d/postgresql

需要更改的地方
这里写图片描述

如果把postgres安装在/usr/local/下面或许就不用更改了吧????

[root@pgserver01 postgresql-9.4.5]# chmod +x /etc/init.d/postgresql

11、启动数据库

[root@pgserver01 postgresql-9.4.5]# /etc/init.d/postgresql start
Starting PostgreSQL: ok
添加开机自启动
[root@pgserver01 postgresql-9.4.5]# chkconfig --add postgresql
[root@pgserver01 postgresql-9.4.5]# chkconfig postgresql on
[root@pgserver01 postgresql-9.4.5]#
# 12、 **创建数据库操作历史记录文件****
[root@pgserver01 postgresql-9.4.5]#  touch /home/postgres/pgsql/.pgsql_history
[root@pgserver01 postgresql-9.4.5]# chown postgres:postgres /home/postgres/pgsql/.pgsql_history
[root@pgserver01 postgresql-9.4.5]#
# 13、测试一下**
[root@pgserver01 postgresql-9.4.5]# su - postgres
mount: only root can do that
[postgres@pgserver01 ~]$ psql --version
psql (PostgreSQL) 9.4.5
[postgres@pgserver01 ~]$
[postgres@pgserver01 ~]$ createdb test
[postgres@pgserver01 ~]$ psql test
psql (9.4.5)
Type "help" for help.
test=# \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
 test      | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)
test=# 

14、开启远程访问

[postgres@pgserver01 ~]$ cd /home/postgres/pgsql/data/
[postgres@pgserver01 data]$ ll
total 120
drwx------. 6 postgres postgres  4096 Nov  8 19:44 base
drwx------. 2 postgres postgres  4096 Nov  8 19:44 global
drwx------. 2 postgres postgres  4096 Nov  8 19:24 pg_clog
drwx------. 2 postgres postgres  4096 Nov  8 19:24 pg_dynshmem
-rw-------. 1 postgres postgres  4468 Nov  8 19:24 pg_hba.conf
-rw-------. 1 postgres postgres  1636 Nov  8 19:24 pg_ident.conf
drwx------. 4 postgres postgres  4096 Nov  8 19:24 pg_logical
drwx------. 4 postgres postgres  4096 Nov  8 19:24 pg_multixact
drwx------. 2 postgres postgres  4096 Nov  8 19:41 pg_notify
drwx------. 2 postgres postgres  4096 Nov  8 19:24 pg_replslot
drwx------. 2 postgres postgres  4096 Nov  8 19:24 pg_serial
drwx------. 2 postgres postgres  4096 Nov  8 19:24 pg_snapshots
drwx------. 2 postgres postgres  4096 Nov  8 19:24 pg_stat
drwx------. 2 postgres postgres  4096 Nov  8 19:48 pg_stat_tmp
drwx------. 2 postgres postgres  4096 Nov  8 19:24 pg_subtrans
drwx------. 2 postgres postgres  4096 Nov  8 19:24 pg_tblspc
drwx------. 2 postgres postgres  4096 Nov  8 19:24 pg_twophase
-rw-------. 1 postgres postgres     4 Nov  8 19:24 PG_VERSION
drwx------. 3 postgres postgres  4096 Nov  8 19:24 pg_xlog
-rw-------. 1 postgres postgres    88 Nov  8 19:24 postgresql.auto.conf
-rw-------. 1 postgres postgres 21286 Nov  8 19:24 postgresql.conf
-rw-------. 1 postgres postgres    67 Nov  8 19:41 postmaster.opts
-rw-------. 1 postgres postgres    82 Nov  8 19:41 postmaster.pid
-rw-r--r--. 1 root     root       241 Nov  8 19:41 serverlog
[postgres@pgserver01 data]$ vim postgresql.conf

添加:

listen_addresses = '*

15、信任远程连接

[postgres@pgserver01 data]$ vim pg_hba.conf

这里写图片描述

16、重启postgreSQL服务

service postgresql-9.5 restart
service postgresql-9.5 status

编译安装OK


17、参考文档:

http://www.centoscn.com/image-text/install/2015/0407/5110.html
http://www.linuxidc.com/Linux/2015-07/120565.htm
http://www.programgo.com/article/67314201868/;jsessionid=F94AF3F4B3CC25D7DAA4BD20EB4D8F95

猜你喜欢

转载自blog.csdn.net/qq_29622761/article/details/53088041