PostgreSQL 导入 CSV 文件

FreeBSD 7.0-RELEASE-i386 + PostgreSQL8.3

# psql -d 数据库 -U 用户名
# \encoding
# show client_encoding;
# set client_encoding = 'iso-8859-1'; //CSV文件字符格式,不然会乱码
# COPY 表名 FROM 'CSV文件' WITH DELIMITER ',' NULL AS '' CSV HEADER QUOTE AS '|';
# \q

Continue Reading

PostgreSQL 处理 session

先来看下自定义数据库处理 session 的执行顺序:

sess_open
sess_read
sess_gc
...其他程序
sess_write || sess_destroy
sess_close

// $domain = '';
//不使用 GET/POST 变量方式
ini_set('session.use_trans_sid', 0);
//设置垃圾回收最大生存时间
ini_set('session.gc_maxlifetime', 1440);
//使用 COOKIE 保存 SESSION ID 的方式
ini_set('session.use_cookies', 0);
//ini_set('session.cookie_path',      '/');
//多主机共享保存 SESSION ID 的 COOKIE
//ini_set('session.cookie_domain', $domain);
//设置用户自定义Session存储
ini_set('session.save_handler', 'user');
ini_set('session.gc_probability', 1);
ini_set('session.gc_divisor', 100);

session_set_save_handler(
	'sess_open',
	'sess_close',
	'sess_read',
	'sess_write',
	'sess_destroy',
	'sess_gc'
);

$SESS_DB = '';
$SESS_DBHOST = '127.0.0.1'; /* database server hostname */
$SESS_DBPORT = 5432; /* database server port */
$SESS_DBNAME = 'DBNAME'; /* database name */
$SESS_DBUSER = 'DBUSER; /* database user */
$SESS_DBPASS = 'DBPASS'; /* database password */
$SESS_LIFE = ini_get('session.gc_maxlifetime');
$SESS_NAME = 'SNAME';


function sess_open($save_path, $session_name){
	global $SESS_DB, $SESS_DBHOST, $SESS_DBPORT, $SESS_DBNAME, $SESS_DBUSER, $SESS_DBPASS;
	$SESS_DB = pg_connect("host=$SESS_DBHOST port=$SESS_DBPORT dbname=$SESS_DBNAME user=$SESS_DBUSER password=$SESS_DBPASS") or die('Could not connect');
	return true;
}

function sess_close(){
	global $SESS_DB;
	if(!empty($SESS_DB)){
		pg_close($SESS_DB);
	}
	return true;
}

function sess_read($key){
	global $SESS_DB, $SESS_LIFE, $uid, $uip;
	自定义
	return false;
}

function sess_write($key, $val){
	global $SESS_DB, $SESS_LIFE, $uid, $ip;
	$expiry = time() + $SESS_LIFE;
	$value = addslashes($val);
	自定义
}

function sess_destroy($key){
	global $SESS_DB;
	自定义
}

function sess_gc($maxlifetime){
	global $SESS_DB;
	自定义
}

安装POSTGRESQL

FreeBSD 7.0-RELEASE-i386

# cd /usr/ports/databases/postgresql83-server && make install clean
# echo ‘postgresql_enable=”YES”‘ >> /etc/rc.conf
# /usr/local/etc/rc.d/postgresql initdb
# passwd pgsql
# su – pgsql
$ createuser -P -s -e XXXX (创建一个新的超级用户)

安装phpPgAdmin
# cd /usr/ports/databases/phppgadmin && make install clean
# ee /usr/local/etc/apache22/Includes/phppgadmin.conf

Alias /phppgadmin/ “/usr/local/www/phpPgAdmin/”

<Directory “/usr/local/www/phpPgAdmin/”>
Options none
AllowOverride Limit

Order Deny,Allow
Deny from all
#Allow from 127.0.0.1 .example.com
Allow from 127.0.0.1 192.168
#Allow from all
</Directory>

# ee /usr/local/www/phpPgAdmin/conf/config.inc.php

$conf[‘default_lang’] = ‘chinese-utf8-zh_CN’;

# apachectl restart

http://localhost/phppgadmin/

安装过程错误解决:
如果先安装了KDE3或者其他软件,再安装postgresql83可能会提示冲突终止安装,比如提示postgresql81-client冲突.
# cd /usr/ports/databases/postgresql81-client && make deinstall
卸载postgresql81-client再重新安装
如果有安装PHP5还要重新安装pgsql.so和pdo_pgsql.so
# cd /usr/ports/databases/php5-pgsql && make deinstall
# cd /usr/ports/lang/php5-extensions && make reinstall clean FORCE_PKG_REGISTER=YES

重启PGSQL
$ pg_ctl restart -D /usr/local/pgsql/data/
重载配置文件
$pg_ctl reload -D /usr/local/pgsql/data/

远程登录

postgresql.conf

listen_addresses = ‘*’

pg_hba.conf

hostssl all all 192.168.0.1/24 md5
hostssl all all 12.12.151.15/32 md5


免费在线记账服务,个人理财好帮手,小型企业财务管理工具。
http://www.keepaccounts.com/