您的位置:首页 > 运维架构 > Linux

Postgresql Linux版本安装——RPM包安装

2016-01-22 11:10 239 查看

 

Postgresql和MySQL是目前比较流行、活跃的开源关系型数据库系统。相对于高端Oracle商业产品,Postgresql和MyQL在软件成熟度、发展功能上的确还有很大改善空间。但是在系统选型过程中,基础软件水平是要受到未来系统整体负载、运维要求和重要的预算决定的。好东西是好,但也要看我们是否需要他,或者是否用得起他。

 

相对于MySQL的粗放式发展,Postgresql从最开始被Berkley研究出来,作为科研机构教学使用以来,无论是从对关系型数据库标准的遵循,还是内核发展上,都本着比较严谨的态度和方式。所以,在MySQL不支持事务的时代,Postgresql是不错的关系型数据库产品方案。

 

目前,Postgresql目前支持包括Windows、Linux和Unix等主流操作系统平台。在Linux上,我们可以使用三种常见的安装方式:

 

ü  RPM包方式:从Postgresql官方网站上,直接下载Linux安装包,通过yum或者rpm进行安装;

ü  Yum安装:连接Postgresql的Yum库,从网站下载所有的安装文件和依赖包;

ü  源代码安装:这种方式灵活性最大,可以显示的自主决定安装目录和数据文件位置;

 

本篇主要介绍RPM安装包安装方法和之后的连接配置方式。

 

1、安装文件准备

 

笔者使用RedHat Linux 6.4进行测试。

 

 

[root@TEST-DB uploads]# cat /etc/redhat-release

Red Hat Enterprise Linux Server release 6.4 (Santiago)

 

 

从PG官方网站下载RPM安装包,网址:http://www.postgresql.org/download/。实验选择最新的9.5版本。

 

 

[root@TEST-DB uploads]# ls -l

total 6580

-rw-r--r-- 1 root root 1373788 Jan 21 08:54 postgresql95-9.5.0-2PGDG.rhel6.x86_64.rpm

-rw-r--r-- 1 root root  465492 Jan 21 08:54 postgresql95-contrib-9.5.0-2PGDG.rhel6.x86_64.rpm

-rw-r--r-- 1 root root  204948 Jan 21 08:54 postgresql95-libs-9.5.0-2PGDG.rhel6.x86_64.rpm

-rw-r--r-- 1 root root 4682132 Jan 21 08:54 postgresql95-server-9.5.0-2PGDG.rhel6.x86_64.rpm

 

 

2、安装程序包

 

请注意安装顺序,原则上说,基础Linux版本就可以完成安装过程。但是对于一些包,可能需要yum程序进行支持。

 

 

[root@TEST-DB uploads]# yum install postgresql95-libs-9 3ff7 .5.0-2PGDG.rhel6.x86_64.rpm

Loaded plugins: product-id, refresh-packagekit, security, subscription-manager

This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.

localyum                                                                 

Dependencies Resolved

 

=================================================================================================

 Package           Arch   Version              Repository                                   Size

=================================================================================================

Installing:

 postgresql95-libs x86_64 9.5.0-2PGDG.rhel6    /postgresql95-libs-9.5.0-2PGDG.rhel6.x86_64 639 k

Updating for dependencies:

 openssl           x86_64 1.0.1e-15.el6        localyum                                    1.5 M

 

Transaction Summary

=================================================================================================

Install       1 Package(s)

Upgrade       1 Package(s)

 

Total size: 2.1 M

(篇幅原因,有省略……)                                                              

 

Complete!

 

 

[root@TEST-DB uploads]# yum install postgresql95-9.5.0-2PGDG.rhel6.x86_64.rpm

Loaded plugins: product-id, refresh-packagekit, security, subscription-manager

This system is not registered to Red Hat Subscription Management. You can use

(篇幅原因,有省略……)

Installed:

  postgresql95.x86_64 0:9.5.0-2PGDG.rhel6                                                       

 

Complete!

 

 

[root@TEST-DB uploads]# rpm -ivh postgresql95-server-9.5.0-2PGDG.rhel6.x86_64.rpm

warning: postgresql95-server-9.5.0-2PGDG.rhel6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY

Preparing...                ########################################### [100%]

   1:postgresql95-server    ########################################### [100%]

[root@TEST-DB uploads]# rpm -ivh postgresql95-contrib-9.5.0-2PGDG.rhel6.x86_64.rpm

warning: postgresql95-contrib-9.5.0-2PGDG.rhel6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY

Preparing...                ########################################### [100%]

   1:postgresql95-contrib   ########################################### [100%]

 

 

 

注意:选择使用rpm包进行安装之后,会自动创建操作系统用户postgres,可以使用passwd命令进行密码设置。

 

 

[root@TEST-DB uploads]# id postgres

uid=26(postgres) gid=26(postgres) groups=26(postgres)

 

[root@TEST-DB uploads]# 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.

 

 

3、本地服务配置和本地连接测试

 

使用RPM包安装之后,系统中会自动添加一个postgresql-<vxxx>的服务。我们可以通过这个服务启动数据库。

 

 

--默认状态是关闭

[root@TEST-DB uploads]# service --status-all | grep postgres

postgresql-9.5 is stopped

 

--强制启动失效

[root@TEST-DB uploads]# service postgresql-9.5 start

/var/lib/pgsql/9.5/data is missing. Use "service postgresql-9.5 initdb" to initialize the cluster first.

[FAILED]

 

 

第一次启动失败,主要是由于最开始数据库中没有主库存在。可以先用initdb来进行初始化动作。

 

 

[root@TEST-DB uploads]# service postgresql-9.5 initdb

Initializing database: [  OK  ]

[root@TEST-DB uploads]# service --status-all | grep postgres

postgresql-9.5 is stopped

[root@TEST-DB uploads]# service postgresql-9.5 start

Starting postgresql-9.5 service: [  OK  ]

 

 

查看后台postgres进程运行情况。

 

 

[root@TEST-DB uploads]# ps -ef | grep postgres

postgres 28417     1  0 09:53 ?        00:00:00 /usr/pgsql-9.5/bin/postmaster -D /var/lib/pgsql/9.5/data

postgres 28419 28417  0 09:53 ?        00:00:00 postgres: logger process                               

postgres 28421 28417  0 09:53 ?        00:00:00 postgres: checkpointer process                         

postgres 28422 28417  0 09:53 ?        00:00:00 postgres: writer process                               

postgres 28423 28417  0 09:53 ?        00:00:00 postgres: wal writer process                           

postgres 28424 28417  0 09:53 ?        00:00:00 postgres: autovacuum launcher process                  

postgres 28425 28417  0 09:53 ?        00:00:00 postgres: stats collector process                      

root     28431 27089  0 09:53 pts/0    00:00:00 grep postgres

 

 

两个细节,标记红色的部分是数据库主工作进程,其中明显表明了可执行程序和数据文件位置。另外就是大部分进程执行用户都是postgres,就说明虽然是通过root启动的程序,但是root也是用postgres用户进程启动程序。

 

本地连接情况,在本地可以使用psql作为客户端进行连接。

 

 

[root@TEST-DB ~]# su - postgres

-bash-4.1$ psql

psql (9.5.0)

Type "help" for help.

 

postgres=# help

You are using psql, the command-line interface to PostgreSQL.

Type:  \copyright for distribution terms

       \h for help with SQL commands

       \? for help with psql commands

       \g or terminate with semicolon to execute query

       \q to quit

postgres=#

 

postgres=# alter user postgres with password 'postgres';

ALTER ROLE

postgres=# select * from pg_shadow;

 usename  | usesysid | usecreatedb | usesuper | userepl | usebypassrls |               passwd   

            | valuntil | useconfig

----------+----------+-------------+----------+---------+--------------+-------------------------

------------+----------+-----------

 postgres |       10 | t           | t        | t       | t            | md53175bce1d3201d16594ce

bf9d7eb3f9d |          |

(1 row)

 

postgres-# \q

-bash-4.1$ exit

logout

 

 

根据标准psql命令,后面应当写上连接数据库的名称。如果没有写,就默认连接名称为postgres的数据库,这个库就是在initdb执行过程中创建好的内容。

 

4、远程连接配置

 

注意:我们当前所有操作都是在数据库服务器上进行的操作。默认情况下,Postgresql对于远程访问连接时拒绝的。我们需要进行额外的配置项目。

 

同MySQL相似,PG的很多配置参数都是散布在文本格式文件中的。在配置网络连接中,我们需要修改两个文件。

 

首先是PG数据文件下的pg_hba.conf。

 

 

[root@TEST-DB ~]# su - postgres

-bash-4.1$ cd /var/lib/pgsql/9.5/data/

 

-bash-4.1$ ls -l | grep conf

-rw------- 1 postgres postgres  4224 Jan 21 09:53 pg_hba.conf

-rw------- 1 postgres postgres  1636 Jan 21 09:53 pg_ident.conf

-rw------- 1 postgres postgres    88 Jan 21 09:53 postgresql.auto.conf

-rw------- 1 postgres postgres 21738 Jan 21 09:53 postgresql.conf

 

 

pg_hba.conf内容很多,我们需要修改其中关于允许连接站点的设置。

 

 

# "local" is for Unix domain socket connections only

local   all             all                                     peer

# IPv4 local connections:

host    all             all             127.0.0.1/32            ident

# IPv6 local connections:

host    all             all             ::1/128                 ident

# Allow replication connections from localhost, by a user with the

# replication privilege.

#local   replication     postgres                                peer

#host    replication     postgres        127.0.0.1/32            ident

#host    replication     postgres        ::1/128                 ident

 

 

在IPv4 Local connection部分进行配置。

 

 

# IPv4 local connections:

host    all             all             127.0.0.1/32            ident

host    all             all             172.17.107.0/24         password

host    all             all             172.17.197.0/24         password

 

 

连接IP地址后面的/24表示可以支持该网段所有IP地址访问。Password表示通过用户名密码验证方式连接。

 

另一个配置文件是postgresql.conf,其中定义了监听程序listener的监听范围。默认情况下取值如下:

 

 

# - Connection Settings -

#listen_addresses = 'localhost'         # what IP address(es) to listen on;

                           # comma-separated list of addresses;

                           # defaults to 'localhost'; use '*' for all

                           # (change requires restart)

#port = 5432                            # (change requires restart)

max_connections = 100                   # (change requires restart)

 

8000  

修改listen_addresses项目,去除掉注释信息,将localhost变为*。这样就控制监听程序监听来自所有IP地址的连接请求了。

 

 

# - Connection Settings -

listen_addresses = '*'          # what IP address(es) to listen on;

                         # comma-separated list of addresses;

                          # defaults to 'localhost'; use '*' for all

                                        # (change requires restart)

#port = 5432                            # (change requires restart)

max_connections = 100                   # (change requires restart)

 

 

重新启动PG程序。

 

 

[root@TEST-DB ~]# service postgresql-9.5 restart

Stopping postgresql-9.5 service: [  OK  ]

Starting postgresql-9.5 service: [  OK  ]

 

 

在远程Windows服务器上,我们通过pgAdmin客户端工具配置好连接界面。

 

 

点击连接,可以确认成功。

 

 

5、结论

 

PG是目前比较流行的开源关系数据库产品。对于大多数的行业和企业而言,关系型数据库其实还是占到需求的主流位置的。


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/17203031/viewspace-1980628/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/17203031/viewspace-1980628/

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: