Postgresql无法远程连接

环境: CentOS 5, Postgresql 9, WinXp

通过winXP上的psql连接远程服务器上postgresql服务,出现错误:
C:\\Documents and Settings\\rand>psql -U postgres -d postgres -h 10.71.88.251 -p 5432
psql: 无法联接到服务器: Connection refused (0x0000274D/10061)
服务器是否在主机 “10.71.88.251” 上运行并且准备接受在端口
5432 上的 TCP/IP 联接?

之前配置过pg_hba.conf上的信息:
host    all             all             10.71.88.252/32         trust    –注10.71.88.252为winxp的IP地址

经排错,发现postgresql.conf中有一个监听器地址参数为#listen_addresses = ‘localhost’, 必须将其修改为listen_addresses = ‘*’,以便可以让其它客户端来访问。

Greenplum安装dblink模块

环境:centos 5, greenplum 4, postgresql 8.2.22
解压后切换到/home/gpadmin/postgresql-8.2.22/contrib/dblink目录下,
$vi Makefile
将PG_CPPFLAGS = -I$(libpq_srcdir) 设置为PG_CPPFLAGS = -I$(libpq_srcdir) -w
$make USE_PGXS=1 install
然后将dblink.so拷贝到其它segment hosts上
$ gpssh -f /host/gpadmin/host_all dblink.so ‘/usr/local/gp/lib/postgresql/dblink.so’
$createdb testdb
$psql -f dblink.sql testdb

Postgresql日志归档设置

环境:winXP, postgresql

当需要对postgresql数据库进行定点恢复的时候,需要设置归档日志。具体如下:

wal_level:设置信息级别至wal.

archive_mode:表示是否进行归档操作,默认值为off(关闭)。

archive_command:由管理员设置的用于归档WAL日志的命令。

archive_time:表示归档周期,在超过该参数设定的时间时强制切换WAL段,默认值为0。

将参数设置wal_level=archive, archive_mode=on, archive_command=’copy “%p” “D:/program/pgsql/archived_log/%f”‘, archive_time=3600

Postgresql的调度pgAgent

环境: winxp, postgresql 9.0 ,  pgAgent-3.0.0

pgAgent是Postgresql的一个调度程序,在之前postgresql 9.0之前一直附带在pgAdmin包下,只是默认不安装。在之后的版本就单独作为一个安装包。在http://www.pgadmin.org/download/pgagent.php可以下载pgAgent程序。将下载的程序解压放在pgAdmin III目录下,运行pgagent.sql脚本,psql -U postgres -f $pginstall\\pgAdmin III\\pgAgent\\pgagent.sql。然后安装pgagent服务,$pginstall\\pgAdmin III\\pgAgent INSTALL pgAgent -u postgres
-p secret hostaddr=127.0.0.1 dbname=postgres user=postgres password=secret

这样在windows服务界面上有PostgreSQL Scheduling Agent – pgAgent服务,启动它才可以运行设定的批处理程序或sql语句。

PostgreSQL归档日志配置

环境: windows sever 2003 , postgresql 8.4

在PostgreSQL的安装目录下找到postgresql.conf文件,将其中的archive_mode , archive_command 修改为如下:

# – Archiving –

archive_mode = on  # allows archiving to be done
# (change requires restart)

archive_command = ‘copy “%p” D:\\\\\\\\program\\\\\\\\postgresql_archivedlog\\\\\\\\”%f”‘  # command to use to archive a logfile segment

archive_timeout = 600  # force a logfile segment switch after this
# number of seconds; 0 disables

 

 

日志切换:

postgresql =# select pg_switch_xlog();

R语言结合H2O做深度学习

环境: Windows 7,  RStudio

1. 进入RStudio,输入安装

 install.packages("h2o", repos=(c("http://s3.amazonaws.com/h2o-release/h2o/rel-kahan/5/R", getOption("repos"))))

2. 加装包,启动h2o本地环境
   library(h2o)
载入需要的程辑包:rjson
载入需要的程辑包:statmod
载入需要的程辑包:tools

----------------------------------------------------------------------

Your next step is to start H2O and get a connection object (named
'localH2O', for example):
    > localH2O = h2o.init()

For H2O package documentation, first call init() and then ask for help:
    > localH2O = h2o.init()
    > ??h2o

To stop H2O you must explicitly call shutdown (either from R, as shown
here, or from the Web UI):
    > h2o.shutdown(localH2O)

After starting H2O, you can use the Web UI at http://localhost:54321
For more information visit http://docs.0xdata.com

----------------------------------------------------------------------

载入程辑包:‘h2o’

下列对象被屏蔽了from ‘package:base’:

    max, min, sum

Warning messages:
1: 程辑包‘h2o’是用R版本3.0.3 来建造的 
2: 程辑包‘rjson’是用R版本3.0.3 来建造的 
3: 程辑包‘statmod’是用R版本3.0.3 来建造的

 

3.  观看下示例
localH2O = h2o.init(ip = “localhost”, port = 54321, startH2O = TRUE,Xmx = ‘1g’)

H2O is not running yet, starting it now…
Performing one-time download of h2o.jar from
http://s3.amazonaws.com/h2o-release/h2o/rel-knuth/11/Rjar/h2o.jar
(This could take a few minutes, please be patient…)

Note:  In case of errors look at the following log files:
C:/TMP/h2o_huangqiang01_started_from_r.out
C:/TMP/h2o_huangqiang01_started_from_r.err

java version “1.7.0_25”
Java(TM) SE Runtime Environment (build 1.7.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)

Successfully connected to http://127.0.0.1:54321
R is connected to H2O cluster:
H2O cluster uptime:        3 seconds 408 milliseconds
H2O cluster version:       2.4.3.11
H2O cluster name:          H2O_started_from_R
H2O cluster total nodes:   1
H2O cluster total memory:  0.96 GB
H2O cluster total cores:   4
H2O cluster healthy:       TRUE

demo(h2o.glm)

4. 训练minist数据

下载 Train Dataset: http://www.pjreddie.com/media/files/mnist_train.csv
下载 Test Dataset: http://www.pjreddie.com/media/files/mnist_test.csv

res <- data.frame(Training = NA, Test = NA, Duration = NA)

#加载数据到h2o
train_h2o <- h2o.importFile(localH2O, path = “C:/Users/jerry/Downloads/mnist_train.csv”)
test_h2o <- h2o.importFile(localH2O, path = “C:/Users/jerry/Downloads/mnist_test.csv”)

y_train <- as.factor(as.matrix(train_h2o[, 1]))
y_test <- as.factor(as.matrix(test_h2o[, 1]))

##训练模型要很长一段时间,多个cpu使用率几乎是100%,风扇狂响。最后一行有相应的进度条可查看
model <- h2o.deeplearning(x = 2:785,  # column numbers for predictors
y = 1,   # column number for label
data = train_h2o,
activation = “Tanh”,
balance_classes = TRUE,
hidden = c(100, 100, 100),  ## three hidden layers
epochs = 100)

#输出模型结果
> model
IP Address: localhost
Port      : 54321
Parsed Data Key: mnist_train.hex
Deep Learning Model Key: DeepLearning_9c7831f93efb58b38c3fa08cb17d4e4e

Training classification error: 0
Training mean square error: Inf

Validation classification error: 0
Validation square error: Inf

Confusion matrix:
Reported on mnist_train.hex
Predicted
Actual      0    1    2    3    4    5    6    7    8    9 Error
0      5923    0    0    0    0    0    0    0    0    0     0
1         0 6742    0    0    0    0    0    0    0    0     0
2         0    0 5958    0    0    0    0    0    0    0     0
3         0    0    0 6131    0    0    0    0    0    0     0
4         0    0    0    0 5842    0    0    0    0    0     0
5         0    0    0    0    0 5421    0    0    0    0     0
6         0    0    0    0    0    0 5918    0    0    0     0
7         0    0    0    0    0    0    0 6265    0    0     0
8         0    0    0    0    0    0    0    0 5851    0     0
9         0    0    0    0    0    0    0    0    0 5949     0
Totals 5923 6742 5958 6131 5842 5421 5918 6265 5851 5949     0
>

> str(model)

## 评介性能
yhat_train <- h2o.predict(model, train_h2o)$predict
yhat_train <- as.factor(as.matrix(yhat_train))

yhat_test <- h2o.predict(model, test_h2o)$predict
yhat_test <- as.factor(as.matrix(yhat_test))

查看前100条预测与实际的数据相比较
> y_test[1:100]
[1] 7 2 1 0 4 1 4 9 5 9 0 6 9 0 1 5 9 7 3 4 9 6 6 5 4 0 7 4 0 1 3 1 3 4 7 2 7 1 2 1 1 7 4 2 3 5 1 2 4 4 6 3 5 5 6 0 4 1 9 5 7 8 9 3 7 4
[67] 6 4 3 0 7 0 2 9 1 7 3 2 9 7 7 6 2 7 8 4 7 3 6 1 3 6 9 3 1 4 1 7 6 9
Levels: 0 1 2 3 4 5 6 7 8 9
>
> yhat_test[1:100]
[1] 7 2 1 0 4 1 8 9 4 9 0 6 9 0 1 5 9 7 3 4 9 6 6 5 4 0 7 4 0 1 3 1 3 4 7 2 7 1 2 1 1 7 4 2 3 5 1 2 4 4 6 3 5 5 6 0 4 1 9 5 7 8 9 3 7 4
[67] 6 4 3 0 7 0 2 9 1 7 3 2 9 7 7 6 2 7 8 4 7 3 6 1 3 6 9 3 1 4 1 7 6 9
Levels: 0 1 2 3 4 5 6 7 8 9
效果还可以

## 查看并保存结果
library(caret)
res[1, 1] <- round(h2o.confusionMatrix(yhat_train, y_train)$overall[1], 4)
res[1, 2] <- round(h2o.confusionMatrix(yhat_test, y_test)$overall[1], 4)
print(res)

(注意:程辑包‘h2o’是用R版本3.0.1 来建造的 , 因此R base应该升级到相应版本, 不然就出现以下报错:

> library(h2o)
Error in eval(expr, envir, enclos) : 没有”.getNamespace”这个函数
此外: 警告信息:
程辑包‘h2o’是用R版本3.0.1 来建造的
Error : 程辑包‘h2o’里的R写碼载入失败
错误: ‘h2o’程辑包/名字空间载入失败

解决方法: 下载http://cran.r-project.org/bin/windows/base/old/3.0.1/R-3.0.1-win.exe 并安装, 更新其它包的 update.packages(ask=FALSE, checkBuilt = TRUE)
)


R 3.0.1 安装包RCurl, rjson

环境: windows 7, R 3.0.1

安装h2o的时候发现无法在线安装这两个包rjson, RCurl,因此把这两个放在迅雷上下载到本地安装
http://ftp.ctex.org/mirrors/CRAN/bin/windows/contrib/3.0/rjson_0.2.14.zip
http://ftp.ctex.org/mirrors/CRAN/bin/windows/contrib/3.0/RCurl_1.95-4.3.zip

install.packages(“d:/Downloads/RCurl_1.95-4.3.zip”, repos=NULL, type=”source”)
install.packages(“d:/tdDownload/RCurl_1.95-4.3.zip”, repos=NULL, type=”source”)

R 3.0.1 安装包RCurl, rjson

环境: windows 7, R 3.0.1

安装h2o的时候发现无法在线安装这两个包rjson, RCurl,因此把这两个放在迅雷上下载到本地安装
http://ftp.ctex.org/mirrors/CRAN/bin/windows/contrib/3.0/rjson_0.2.14.zip
http://ftp.ctex.org/mirrors/CRAN/bin/windows/contrib/3.0/RCurl_1.95-4.3.zip

install.packages(“d:/Downloads/RCurl_1.95-4.3.zip”, repos=NULL, type=”source”)
install.packages(“d:/tdDownload/RCurl_1.95-4.3.zip”, repos=NULL, type=”source”)