Friday, February 13, 2009

postgres学习之(2): database管理之用户与DB

为方便日后数据迁移. 一定要学会postgres的管理, 不然, 处于被动环境.

信息来源: <> 和 伟大的man手册
postgres的管理都可以通过交互前端psql命令去完成.

但是为了方便, 还有几个细化的命令(这此命令实际上就是SQL的wrapper!):
1. createdb
注: 一旦创建了一个数据库, 创建者就是该数据库的拥有者和管理员. 一般情况下为用户pgsql, linux下应该为postgres, 因为pgsql一般是数据库管理员使用的帐号, 当然, 不使用pgsql用户权限也可以正常使用createdb, 这取决于postgres的认证方式. 否则会出现下面的信息:

[jessinio@study ~]$ psql
psql: FATAL: database "jessinio" does not exist

[jessinio@study ~]$ createdb jessinio
createdb: could not connect to database postgres: FATAL: role "jessinio" does not exist

上面的主要信息是: jessinio无法使用createdb, 这是因为在postgres服务中, 没有jessinio这个role(角色)!

role表示了:要连接和使用数据库, 第一条件是要在postgres系统中存在相应的role才能登陆!
* 注意: 有了相应的role, 并不等于有了root权限(superuser)!
* 权限总的来说有两种: 对DBMSs的权限, 还有一种是对DB的权限

role如何去创建与修改, 新创建的role对DBMSs有哪些权限?

答案是:createuser

下面是一些选项和它们的默认值:
--superuser
--no-superuser: Ture
--createdb
--no-createdb: Ture
--createrole
--no-createrole: Ture
--login: Ture
--no-login
--inherit
--no-inherit: Ture
--connection-limit: no limit
--encrypted
--unencrypted: Ture
* Ture表示默认值.

下面是实际情况:

[pgsql@study /usr/home/jessinio]$ createuser jessinio
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) n
CREATE ROLE

好了, 现在有jessinio用户有了对DBMSs操作的role了, createdb吧:
[jessinio@study ~]$ createdb jessinio
CREATE DATABASE

一切顺利!!!!!

去哪里使用万能的SQL呢? 答案是: psql

[jessinio@study ~]$ psql
Welcome to psql 8.2.9, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit

jessinio=>

进入了.

留意一下, 就可以知道, jessinio用户登陆是不用输入密码的. 为什么呢?

这与postgres的认证系统有关. postgres的认证方式写在下一篇blog中.

Thursday, February 12, 2009

postgres学习之(1): postgres的安装

工作中需要使用到postgres, 为了方便, 决定学习一把postgres的安装与管理!(日后不怕事呀 ^_^)
* 文章内容全是个人摸索过程, 废话比较多. 应该少看为好 (^_^)

Step One:
当然是安装了, 在FreeBSD下, postgres的server Port的版本需要与postgres的client Port版本一一对应的, 就是说:
* postgres的client8.2就要与postgres的server8.2对应, 不过安装会出错. 例子如下:
本人的机器中早已经安装了postgres82-client. 当本人想去安装postgres83-server时就出错, 解决方法有二:
1. pkg_delete 8.2的client, 安装8.3的client
1. 安装8.2的server

本人选择了第二种, ^_^反正是学习, 方便就行了.

安装完server后, 就会增加一个系统用户名:pgsql
* 注意, 在linux系统下, 此用户名应该是postgres! 我是相当地 囧!!

Step Two:
check一把配置文件. FreeBSD下是/usr/local/pgsql, 不过, 现在还不知道从何入手.

小白的我只知道系统下管理postgres操作是使用系统的特殊用户: 那就是pgsql了!!

切换过去吧:
# su pgsql
$ bash
[pgsql@study /]$
* 这样就使用pgsql用户了. 这是有几个pgsql用户可以使用的命令:
pg_config: retrieve information about the installed version of Postgres
pg_ctl: start, stop, or restart a PostgreSQL server
pg_dumpall: extract a PostgreSQL database cluster into a script file
pg_restore: restore a PostgreSQL database from an archive file created by pg_dump
pg_controldata: display control information of a PostgreSQL database cluster
pg_dump: extract a PostgreSQL database into a script file or other archive file
pg_resetxlog: reset the write-ahead log and other control information of a PostgreSQL database cluster

逐一man一次! ^_^ 无敌的man呀~!~!~!~

发现man不是万能的, 还是看书吧!囧!!

在书上找到了一点灵感, 就是使用pkg_info


pkg_info -L postgresql-server-8.2.9|less

看到了一个initdb, man了一下, 看到cluster这个名词, 这不是pg_*系列命令中的一个有这个单词的吗?

哈, 就是pg_controldata了, 看来两者相关!

每个数据库都有一个叫data的数据目录的, 新安装的postgres还没有看到, 看到是使用initdb的时候了.

好晕, initdb一堆ooptions! 其实, initdb只有个option是必选的: --pgdata=directory

不理, 试一下简单的:

[pgsql@study /usr/ports/databases/postgresql82-server]$ initdb --pgdata=/usr/local/pgsql/
The files belonging to this database system will be owned by user "pgsql".
This user must also own the server process.

The database cluster will be initialized with locales
COLLATE: C
CTYPE: zh_CN.UTF-8
MESSAGES: C
MONETARY: C
NUMERIC: C
TIME: C
The default database encoding has accordingly been set to UTF8.

initdb: directory "/usr/local/pgsql" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/pgsql" or run initdb
with an argument other than "/usr/local/pgsql".

* 发现不行, pgdata目录一定要为空的, 结果使用/usr/local/pgsql/data就OK了

成功initdb后, 有如下内容:

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the -A option the
next time you run initdb.

Success. You can now start the database server using:

postgres -D /usr/local/pgsql/data
or
pg_ctl -D /usr/local/pgsql/data -l logfile start

* 从上面的info可以得到三个信息:
1. 配置文件为pg_hba.conf
1. postgres的启动方式
1. 现在这个cluster的认证方式
* 配置配置文件就不能猜了, 只能看手册了.


本人只修改两个文件:
* pg_hba.conf
* postgresql.conf

postgres服务正常启动! good boy

使用CPAN安装perl包

本来不想搞的. 就是因为在FreeBSD下安装xfce时要求几个perl模块:

1. URI::Escape
1. URI::URL
1. URI::file


cpan命令行的man手册:

* linux下的cpan命令与FreeBSD下的cpan命令在参数的数目上有不同(linux上的参数较多)

cpan命令行总的来说只有两个方式使用:

1. 对特定的module进行操作
# with arguments and no switches, installs specified modules
cpan module_name [ module_name ... ]
* 这是只有module名, 没有使用参数

# with switches, installs modules with extra behavior
cpan [-cfimt] module_name [ module_name ... ]
* 这是使用了参数

2. 对cpan操作
# without arguments, but some switches
cpan [-ahrvACDLO]

对于第一种: 对特定的modules进行操作, 也就是: Module options
有四种情况:
* c 等于make clean
* i 等于install
* m 等于make
* t 等于make test


对于第二种: cpan操作, 也就是: Meta Options
有四种情况:
* a 生成CPAN.pm包
* h 帮助信息
* r 生新编译包
* v 版本信息

没有专业的味道

一下搞FreeBSD, 一下搞Linux, 我都不知道自己是哪一边的了.

现在又搞起了FreeBSD的desktop环境.

呃......

没有专业的味道.....

无语.....

使用FreeBSD做桌面

人就是要折腾的! 手上多出一台机, 反正没有玩过freeBSD下的Xorg, 就玩一下吧.



[jessinio@study /usr/ports/x11/xorg]$ make fetch
This is a meta-port, meaning that it just depends on its subparts of the port.
It won't build and install all the parts until you have typed make install
This port does not ensure things are upgraded; use portupgrade if you want to
upgrade X.Org. If you simply type 'make install' it may use over 2GB to build
all of the subports. You can install the ports singly if you are low on space.
===> Vulnerability check disabled, database not found

呃...... 安装xorg要2GB? 汗!

只能慢慢的等待安装了:
# make install |tee ~/xorg.log


因为前段时间使用了gentoo, 所以在FreeBSD下安装桌面环境还是属于轻车熟路的.

安装四个包就OK了:
1. /usr/port/x11/xorg
1. /usr/port/x11-wm/xfce4
1. /usr/port/chinese/zh-scim-tables
1. /usr/port/www/firefox3
1. /usr/port/chinese/stardict
1. /usr/port/net/rdesktop


只是在安装xfce4时出现了点问题: xfce4需要使用到perl的几个模块, 但是使用cpan安装这几个包是时, 总是在test阶段过不了. 一气之下, 使用force去强制安装. 结果包是安装上了, 不良后果还没有出现. 问题的解决方法还没有找到. 下次使用cpan碰到再解决.


这就FreeBSD的桌面环境起来了!(至少对我来说是这样)

还要增加的软件集为:
1. pidgin
1. QQ
1. ??(暂时还想不出来)

呃..... 好像我只使用这几个软件呢...... 看来是人Out了!!!!

ssh登陆时出错

例子如下:

当我ssh到某一台机器时, ssh会出现下面这种情况:

jessinio@jessinio-desktop:~$ ssh inter0
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the DSA host key has just been changed.
The fingerprint for the DSA key sent by the remote host is
46:3c:fc:26:78:b7:e8:a2:4c:e4:ab:c5:65:11:43:e0.
Please contact your system administrator.
Add correct host key in /home/jessinio/.ssh/known_hosts to get rid of this message.
Offending key in /home/jessinio/.ssh/known_hosts:52
DSA host key for inter0 has changed and you have requested strict checking.
Host key verification failed.

这种事常常会发生,特别是SA.
平时我的解决方法是rm ~/.ssh/known_hosts

不过, 这样有个问题:以前登陆过的机器在ssh登陆时,都会出现:
Are you sure you want to continue connecting (yes/no)?

有点烦!

今天细看了一下上面的ssh抛出的信息(平时我都没有细看过 囧!! ):
Offending key in /home/jessinio/.ssh/known_hosts:52

结果把~/.ssh/known_hosts的第52行delete后, 一切OK

Sunday, February 8, 2009

freeBSD下mount光驱

记录此事, 是因为在FreeBSD工作中, 发现FreeBSD下的光驱与linux的dev不一样.

linux下是/dev/cdrom, 但是在FreeBSD下一时找不得对应功能的dev设备.

google了一把, 发现了比较有意思的mount命令使用方法:

# mount /cdrom

这就是freeBSD方便mount上cdrom的方法.这是怎么做到的呢?

答案是: 得用/etc/fstab

看一下机器的fstab:
/dev/acd0 /cdrom cd9660 ro,noauto 0 0

这样, 在mount命令后加一个path, mount就会去找/etc/fstab对应的dev和mountpoint了.

这等同于:
# mount_cd9660 /dev/acd0 /cdrom
也同于:
# mount -t iso9660 /dev/acd0 /cdrom


BTW:

1. FreeBSD下的光驱dev为acd
2. mount -a 也是利用/etc/fstab