---
title: "监控Postgres数据库"
author: "toplchx"
date: "2015-07-28T21:05:16Z"
tags: ["PostgreSQL", "监控"]
categories: ["数据库"]
slug: "postgres"
draft: false
lastmod: "2015-07-29T11:33:40Z"
id: 2230977
url: "https://toplchx.iteye.com/blog/2230977"
---

官方文档中有关监控的章节：

[http://www.postgresql.org/docs/9.3/static/monitoring.html](http://www.postgresql.org/docs/9.3/static/monitoring.html)

 

监控软件的罗列文档：

[https://wiki.postgresql.org/wiki/Monitoring](https://wiki.postgresql.org/wiki/Monitoring)

 

其中选择安装了powa。这个可以实时监控数据库，但是没有系统cpu和内存图表。

大多数带有系统监控的，都是通过插件实现的，要多安装和配置一些东西。

 

powa的官方网站：

[http://dalibo.github.io/powa/](http://dalibo.github.io/powa/)

 

下面是安装powa的步骤，Centos系统。

 

1、下载解压powa

在官网下载代码，现在是2.0版，这个版本需要postgresql是9.4以上版本。

我的pg是9.3版，所以下载的1.2.1版本

 

wget https://github.com/dalibo/powa/archive/REL\_1\_2\_1.zip  
unzip powa-REL\_1\_2\_1.zip

下载并解压

注：如果没有unzip命令，通过yum安装。（yum install unzip）

 

2、安装postgresql数据库9.3版本

这里不细述了。网上资料很多

 

3、安装powa

cd ./powa-REL\_1\_2\_1  
make

 

一切正常出现类似如下信息

```
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -fpic -I. -I. -I/home/thomas/postgresql/postgresql-9.3.4/include/server -I/home/thomas/postgresql/postgresql-9.3.4/include/internal -D_GNU_SOURCE -I/usr/include/libxml2   -c -o powa.o powa.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -g -fpic -L/home/thomas/postgresql/postgresql-9.3.4/lib -Wl,--as-needed -Wl,-rpath,'/home/thomas/postgresql/postgresql-9.3.4/lib',--enable-new-dtags  -shared -o powa.so powa.o
```

 

安装

```
make install
```

 

4、创建powa用的数据库

进入postgresql执行下列命令

```
$>psql
psql (9.3.5)
Type "help" for help.
postgres=# create database powa;
CREATE DATABASE
postgres=# \c powa
You are now connected to database "powa" as user "postgres".
powa=# create extension pg_stat_statements ;
CREATE EXTENSION
powa=# create extension btree_gist ;
CREATE EXTENSION
powa=# create extension powa;
CREATE EXTENSION
powa=# \dt
                        List of relations
  Schema |              Name               | Type  |  Owner
--------+---------------------------------+-------+----------
  public | powa_functions                  | table | postgres
  public | powa_last_aggregation           | table | postgres
  public | powa_last_purge                 | table | postgres
  public | powa_statements                 | table | postgres
  public | powa_statements_history         | table | postgres
  public | powa_statements_history_current | table | postgres
(6 rows)
```

 

 

5、修改postgresql配置

修改postgressql.conf文件

```
shared_preload_libraries = 'powa,pg_stat_statements'# (change requires restart)
```

```
....track_io_timing= on
```

 

允许本地用户连接powa数据库，在数据库配置文件pg\_hba.conf中添加

host powa all 127.0.0.1/32 md5

重启数据库

```
service postgresql-9.3 restart
```

```
或者
```

```
pg_ctl restart -m f
```

 

至此服务端检测部分安装完成，下面是客户端查看部分。

 

6、安全性

注意客户端的连接安全性。有以下几点可以考虑：

·检查pg\_hba.conf文件

·不要允许用户通过网络连接powa

·不要允许用户通过网络连接数据库

·以https方式连接powa

·使用SSL连接powa的GUI和Postgresql

·拒绝非保护的连接

 

7、需要的前提程序

·Perl 5.10

·Perl DBI and DBD-Pg modules

·Mojolicious 4.75 and later （or A CGI/Perl webserver）

以上版本是测试过的，更老的版本可能可以运行。

 

A)安装Perl

一句安装（各系统不同）：yum install perl

或者手动安装：

下载你需要的版本，下载地址：http://www.activestate.com/activeperl/downloads

% tar zxf ActivePerl-5.18.4.1804-i686-linux-64int-glibc-2.5-298913.tar.gz  
% cd ActivePerl-5.18.4.1804-i686-linux-64int-glibc-2.5-298913  
% ./install.sh

 

安装过程中回答几个问题，最重要的就是安装目录

Did you read the LICENSE.txt file? \[no] yes  
Do you agree to the ActivePerl is covered by the ActiveState Community License.? \[no] yes

Enter top level directory for install? \[/opt/ActivePerl-5.18] /opt/ActivePerl-5.18

Install HTML documentation \[yes] yes

Proceed? \[yes] yes

 

设置环境变量

\# cd /etc/profile.d  
\# vi perl.sh

 

#set perl environment  
#set perl environment  
PERL5LIB=/opt/ActivePerl-5.18/lib:/opt/ActivePerl-5.18/site/lib  
PATH=/opt/ActivePerl-5.18/bin:/opt/ActivePerl-5.18/site/bin:$PATH  
export PERL5LIB  PATH

 

\# source perl.sh

 

初始化CPAN

cpan  
cpan&gt;o conf init urllist

让他自动配置就好了

sites for you? (This means connecting to the Internet) \[yes] yes

最后提交

cpan&gt;o conf commit

 

B)安装DBI

DBI一般都同时安装了，查看是否安装

cpan  
cpan&gt;m DBI

 

C)安装DBD-Pg

cpan  
cpan&gt;install Bundle::DBD::Pg

 

D)安装Mojolicious

下载地址：http://backpan.perl.org/authors/id/S/SR/SRI/Mojolicious-4.75.tar.gz

tar zxf Mojolicious-4.75  
cd Mojolicious-4.75  
perl Makefile.PL PREFIX=/path/to/powa/mojo  
make  
make install

 

8、配置powa

 

cd /path/to/powa/ui/  
cp powa.conf-dist powa.conf  
vi powa.conf

修改"servers"里的内容，数据库的IP端口什么的，可以增加服务器子节点

 

9、启动powa

/path/to/powa/mojo/bin/morbo /path/to/powa/ui/script/powa

 

10、登陆

http://ip:3000

登陆用户密码是数据库的用户密码