存档

2012年2月 的存档

postgres实现导出和导入

2012年2月21日 16hot 没有评论

用postgres 的pg_dump可以实现从从postgres数据库中导出数据。
[1]只导出所有对象的数据库结构
C:\>pg_dump -f DDDDDD.sql -i -C -E UTF8 -n public -s -U portal -h localhost -W portal
-i 是为了兼容数据库版本
-C 包括创建数据库的语句
-E 设定导出数据的编码
-n 是Scheme的名称
-U 是用户名称
-h 是数据库服务器的名称
-W 是用强制密码验证

-s 只导出数据库结构

最后一个参数,当然就是数据库名称了

[2]导出所有对象的数据库结构和数据
C:\>pg_dump -f DDDDDD.sql -i -C -E UTF8 -n public -U portal -h localhost -W portal
没有-s参数
[3]只导出所有的表数据
C:\>pg_dump -f DDDDDD.sql -i -a -C -E UTF8 -n public -U portal -h localhost -W portal
-a 只导出数据
数据导入
[1]c:\psql -f DDDDDD.sql -h 192.168.1.233 -U myuser -W myportal
执行就可以实现导入了。
如果数据库myportal 不存在,要先创建数据库
createdb -U postgres -h 192.168.1.233 myportal
然后再执行上面的导入语句就可以了。
[2]psql -hlocalhost -U myuser -d myportal < DDDDDD.sql
执行语句导入数据就可以了。

分类: PostgreSQL 标签:

影响postgresql性能的几个重要参数(ZT)

2012年2月19日 16hot 没有评论

本人现在开发的所有项目都使用postgresql,应用下来对它很是满意,现就影响postgresql性能的几个重要参数介绍如下,希望对PG的初学者有所帮助,如果你在实际应用中遇到什么问题,可给我留言,我们一起解决:
PG的配置文件是数据库目录下的postgresql.conf文件,8.0以后的版本可支持K,M,G这样的参数,只要修改相应参数后重新启动PG服务就OK了。

shared_buffers:这是最重要的参数,postgresql通过shared_buffers和内核和磁盘打交道,因此应该尽量大,让更多的数据缓存在shared_buffers中。通常设置为实际RAM的10%是合理的,比如50000(400M)

work_mem: 在pgsql 8.0之前叫做sort_mem。postgresql在执行排序操作时,会根据work_mem的大小决定是否将一个大的结果集拆分为几个小的和 work_mem查不多大小的临时文件。显然拆分的结果是降低了排序的速度。因此增加work_mem有助于提高排序的速度。通常设置为实际RAM的2% -4%,根据需要排序结果集的大小而定,比如81920(80M)

effective_cache_size:是postgresql能够使用的最大缓存,这个数字对于独立的pgsql服务器而言应该足够大,比如4G的内存,可以设置为3.5G(437500)

maintence_work_mem:这里定义的内存只是在CREATE INDEX, VACUUM等时用到,因此用到的频率不高,但是往往这些指令消耗比较多的资源,因此应该尽快让这些指令快速执行完毕:给maintence_work_mem大的内存,比如512M(524288)

max_connections: 通常,max_connections的目的是防止max_connections * work_mem超出了实际内存大小。比如,如果将work_mem设置为实际内存的2%大小,则在极端情况下,如果有50个查询都有排序要求,而且都使 用2%的内存,则会导致swap的产生,系统性能就会大大降低。当然,如果有4G的内存,同时出现50个如此大的查询的几率应该是很小的。不过,要清楚 max_connections和work_mem的关系。

分类: PostgreSQL, 转载 标签:

PostgreSQL and Lua

2012年2月16日 16hot 没有评论

Today was the third meeting of the OrlandoPg Meetup group that I host. A member Steven wanted to talk about how he could interact with PostgreSQL using Lua. I informed the group about PostgreSQL’s architecture that allows stored procedures to be written in nearly any programming language. We also talked about how pretty much every language I can think of has a database interaction library of some sort.

That led to some quick googling and we spent the rest of the meeting in a coding dojo like environment to see if we could set up and work with PL/Lua and luasql-postgres. This post will detail how to set up the environment, create a simple stored procedure in PL/Lua, and write a program using the luasql-postgres package or “rock”.

The set up: I’m using a mac for development. OS 10.6.6 to be exact.

To install the Lua programming language I used Mac-Ports.
“sudo port install luarocks +curl openssl”

is the command I used. I actually already had lua installed, so I just installed the luarocks package manager. If you do not have lua installed, this command will install it as well.

Next I needed to get the lua package (rock) luasql-postgres.
“sudo luarocks install –from=http://luarocks.org/repositories/rocks-cvs/ luasql-postgres POSTGRES_DIR=/usr/local/pgsql”

makes that happen. This assumes you already have PostgreSQL installed. If you don’t use macports to install that and adjust your POSTGRES_DIR accordingly. I always install PostgreSQL from source myself.

Next we need to get the PL/Lua extension for PostgreSQL. It can be downloaded from: PgFoundry Once you have it un-tar it and edit the Makefile. Adjust the lines:

LUAINC = -I/opt/local/include

LUALIB = -L/opt/local/lib -llua

Save the file the run
“sudo make && make install”

You now need to install the language into your database. Head over to your contrib directory, mine is:

“/usr/local/pgsql/share/contrib/”
and run

“psql -f pllua.sql DATABASENAME”.

This registers the language handler within your database.

Now you can create a stored procedure using Lua. Example:

create or replace function hello(_name text) returns text as
$$
return string.format(“Hello, %s!”, _name)
$$ Language pllua;

Now you can create a simple lua script to call this function. Example:

require “luasql.postgres”

env = assert ( luasql.postgres() )

con = assert ( env:connect( “DBNAME”,”USERNAME”,”PASS”,”DBHOST” ) )

cur = assert ( con:execute( “SELECT * FROM hello(‘World!’);” ) )

row = assert ( cur:fetch() )

print(row)

One non-standard thing I had to do to get this script to “see” the luasql.postgres extension was to make a symlink:

“sudo ln -s /opt/local/var/luarocks/lib/lua/5.1/luasql/postgres.so /opt/local/lib/lua/5.1/luasql.so”

This is necessary because MacPorts and LuaRocks are not really working together.

You can execute the lua script with the command “”lua SCRIPTNAME” and get the expected output of:

References:

http://www.nessie.de/mroth/lunit/

http://www.lua.org/manual/5.1/manual.html

http://pllua.projects.postgresql.org/

http://www.keplerproject.org/luasql/index.html

分类: LUA, Mac OSX, MacOSX, PostgreSQL 标签: , ,

入手MacBook Pro

2012年2月14日 16hot 没有评论

前两天升级Xorg的时候,又出问题了,折腾了一整天才搞定。加上笔记本的CPU风扇又快要罢工了,动不动的狂响。在过年前就已经有几次启动不了,提示CPU风扇错误。

实在不想太折腾了,时间精力有限。决定买台MacBook 笔记本,希望可以省去一些折腾。

下午就买回来了,不过是晚上才开箱使用的。苹果的产品确实是太精致了,很多别人想不到的细节,他们都考虑到了。第一感觉,用叹为观止来形容并不为过。

接下来还得折腾段时间才能熟悉这个新系统的使用。毕竟使用FreeBSD作为桌面使用了6年多时间,要使用MacOSX 又是得花时间来熟悉和习惯。

摸索ing

分类: 16hot 杂记 标签: