七的博客

Linux 环境下编译以及安装 pgAgent 4.0.0

LinuxPostgreSQL

Linux 环境下编译以及安装 pgAgent 4.0.0

我的编译环境

  • 操作系统: Ubuntu 16.04 LTS ( 64 位)

  • 数据库: Postgresql 9.5

  • pgAgent 源代码版本 : 4.0.0

  • shell : zsh 5.1.1 (x86_64-ubuntu-linux-gnu)

  • cmake version : 3.5.1

以下是源码里面提出的必须满足的环境: - A C/C++ compiler, such as GCC or Microsoft Visual C++ on Windows. - CMake 2.6 or higher installation - A Boost library 1.41 or higher installation - A PostgreSQL 8.3 or higher installation

需要了解的一些技能点

  • Linux 系统基本的跳转指令
  • wget ,tar ,sudo ,apt ,make ,vim 等命令的使用
  • Linux 环境变量的含义以及如何去设置环境变量
  • 科学上网

pgAgent简介

简单的来说 pgAgent 就是 Postgres 数据库中一个任务调度代理工具,它能够运行多个步骤的任务或者是 shell 脚本亦或者是复杂的 SQL 任务。执行任务的时间是非常灵活的,类似于 LInux 下的 cron 表达式。

在 pgAdmin v1.9之前,它是集成在了 pgAdmin 中。从 1.9 版本以后是独立出来的,需要自己手动去安装以及配置。需要注意的是它类似于插件,只能在 pgAdmin 中进行使用。

编译环境准备

Postgresql 安装

编译 pgAgent 必须在安装好数据库的前提下,务必先安装。

sudo apt-get update

## 不设定版本 Ubuntu 16 中会默认安装 PGSQL 9.5版本
sudo apt-get install postgresql

pgAgent 源码

## 下载 pgAgent 源码
wget https://ftp.postgresql.org/pub/pgadmin/pgagent/pgAgent-4.0.0-Source.tar.gz

## 解压
tar zxvf  pgAgent-4.0.0-Source.tar.gz

cmake 安装

## 下载 cmake 源码 
wget https://cmake.org/files/v3.5/cmake-3.5.1.tar.gz

## 编译&&安装命令
./bootstrap && make && make install

## 验证是否成功安装
cmake --version

wxGTK 安装

## 下载 wxGTK 源码
wget https://github.com/wxWidgets/wxWidgets/releases/download/v3.1.2/wxWidgets-3.1.2.tar.bz2

## 解压
tar xvjf wxWidgets-3.1.2.tar.bz2

接下来需要操作用户环境变量,我这里直接编辑的是系统环境变量文件,对所有用户都生效。在线上环境中不建议直接编辑此文件,可以编辑用户级别的环境变量文件。

vim /etc/profile

## 如果上面命令没有权限请先提权
sudo vim /etc/profile

往里面添加以下内容:

#set wx GTK
PATH=$PATH:$HOME/bin:/usr/local/wxGTK-3.1.2/bin
export LD_LIBRARY_PATH=/usr/local/wxGTK-3.1.2/lib:$LD_LIBRARY_PATH

然后执行生效命令:

source /etc/profile

接下来在编译以及安装:

./configure   --enable-shared=no --enable-unicode=yes --prefix=/usr/local/wxGTK-3.1.2

可能会出现的错误:

  1. 缺少 gtk 库
   configure: error: 
   The development files for GTK+ were not found. For GTK+ 2, please
   ensure that pkg-config is in the path and that gtk+-2.0.pc is
   installed. For GTK+ 1.2 please check that gtk-config is in the path,
   and that the version is 1.2.3 or above. Also check that the
   libraries returned by 'pkg-config gtk+-2.0 --libs' or 'gtk-config
   --libs' are in the LD_LIBRARY_PATH or equivalent.

直接从仓库中安装:

   sudo apt install  libgtk2.0-dev

如果出现了以下相类似文字说明安装成功:

   Configured wxWidgets 3.1.2 for `x86_64-pc-linux-gnu'
   
     Which GUI toolkit should wxWidgets use?                 GTK+ 2 with support for GTK+ printing
     Should wxWidgets be compiled into single library?       no
     Should wxWidgets be linked as a shared library?         no
     Should wxWidgets support Unicode?                       yes (using wchar_t)
     What level of wxWidgets compatibility should be enabled?
                                          wxWidgets 2.8      no
                                          wxWidgets 3.0      yes
     Which libraries should wxWidgets use?
                                          STL                no
                                          jpeg               builtin
                                          png                sys
                                          regex              builtin
                                          tiff               builtin
                                          lzma               no
                                          zlib               sys
                                          expat              sys
                                          libmspack          no
                                          sdl                no

编译

进入到 pgAgent 源码文件夹,然后执行以下命令:

cmake ./

可能会碰到的错误:

  1. Boost library 库无法找到,报错可能类似于以下:
   CMake Error at cmake/FindBoost.cmake:2044 (message):
     Unable to find the requested Boost libraries.
   
     Unable to find the Boost header files.  Please set BOOST_ROOT to the root
     directory containing Boost or BOOST_INCLUDEDIR to the directory containing
     Boost's headers.
   Call Stack (most recent call first):
     CMakeLists.txt:112 (FIND_PACKAGE)
   
   
   CMake Error at CMakeLists.txt:118 (MESSAGE):
     Boost library not found.
   
   
   -- Configuring incomplete, errors occurred!

既然提示这么明显,那就直接安装,执行以下命令

   sudo apt-get install libboost-all-dev
  1. 找不到 Postgresql 安装位置,报错可能类似于以下:
   CMake Error at cmake/FindPG.cmake:134 (MESSAGE):
     No PostgreSQL installation could be found.
   Call Stack (most recent call first):
     CMakeLists.txt:98 (FIND_PACKAGE)

这里就是需要配置 Postgresql 相关的环境变量了,依旧是编辑 /etc/profile 文件

   vim /etc/profile
   
   ## 如果上面命令没有权限请先提权
   sudo vim /etc/profile

添加的内容格式为:

   #set postgres
   ## 这个是数据库安装目录,也就是带有 bin 目录的父文件夹,根据自己的情况修改
   export PGDIR=/usr/lib/postgresql/9.5
   ## 这个是数据存放的文件夹,根据自己的情况修改
   export PGDATA=/var/lib/postgresql/9.5/main/data
   ## 这个可以不用改
   export PATH=$PATH:$PGDIR/bin

生效命令:

   source /etc/profile

上面这两个错是最常见的错误了,到此再次编译应该是可以通过编译,就绪执行安装命令

   make 
   
   # 安装
   make install

出现以下类似文本时则为成功安装:

   -- Install configuration: ""
   -- Installing: /usr/local/bin/pgagent
   -- Installing: /usr/local/share/pgagent.sql
   -- Installing: /usr/local/./README
   -- Installing: /usr/local/./LICENSE
   ..........

注意事项

  • 文章中涉及 apt install类似的命令为 Debian 系统安装软件的命令,典型的派生Linux发行版 Ubuntu就是可以使用这个命令安装仓库上面的软件。类似于别的 Linux 发行版可以切换成相对应包管理工具进行安装,例如 Centos 可以使用 yum install达到同样的效果。

  • shell 如果跟我这里一样使用的是 zsh 的话,务必在每个终端打开时执行一次刷新环境命令。

  # 这里刷新的可以是 /etc/profile ,也可以是 ~/.bash_profile 或者是  ~/.bashrc
  # 取决于你配置环境变量到底是配置在哪个文件里面
  source /etc/profile

这是因为使用了 zsh 后终端的启动行为被改变了,并不会去执行 ~/.bash_profile~/.bashrc等脚本了。我这边解决方式就是修改 zsh 的配置文件 ~/.zshrc

  vim  ~/.zshrc
  
  ## 添加的文件内容
  	
  source /etc/profile
  source ~/.bash_profile
  source ~/.bashrc
  • 网上很多博客上内容关于编译 pgAgent 的内容大多数是粘贴复制过来的,错误的地方太多了。尤其是配置 Postgresql 环境变量的配置方式更是错误。所以这里推荐的搜素资料倾向于优先查看官网的文档,然后再推荐搜索引擎。

写在最后

文章中也许会有表达不当的地方,如果发现文章中有什么问题欢迎邮件指出。