全部博文(36)
分类: Mysql/postgreSQL
2012-04-17 18:07:50
PostgreSQL安装
在Linux下安装Postgresql9.1.3,源码安装方式过程如下。
MySQL 是一条轻快的小海豚,但是缺少很多现代关系数据库应有的特色,例如:引用完整性,视图,触发器等。因此,如果你需要开发一个电子商务的网站,需要这些功能的话,你或许应该考虑 PostgreSQL 了。PostgreSQL8.x版本的安装已经非常的简单了。EnterpriseDB制作了一键安装的版本,包括FreeBSD/Linux/Mac OS X/Solaris/Windows平台都有。不过即使使用源码编译,也非常的简单。各个版本的源码可以点这里下载。
1. 创建os用户#useradd postgres
#su - postgres
2. 编译安装安装的过程并不复杂和其他源码版本的安装方法类似:
解压到 /usr/local/:
# tar xvfz postgresql-9.1.3.tar.gz
# cd postgresql-9.1.3
# ./configure --prefix=/usr/local/pgsql
# make
# make install
注:常用选项
$./configure --prefix=/OPT/postgresql --enable-profiling --with-blocksize=8 --with-wal-blocksize=8
其中with-blocksize指定数据块大小,默认8k,with-wal-blocksize指定日志块大小,默认也是8k。更多编译配置选项,可以通过./configure –help查看。
3. 用户权限[root@blue local]# chmod a+w pgsql/
或者 # chown -R postgre.postgre /usr/local/pgsql
4. 初始化database,注意PostgreSQL在服务端不支持GBK编码。[root@blue local]# su postgres
[postgres@blue local]$ cd pgsql/
[postgres@blue pgsql]$ ls
bin include lib share
[postgres@blue pgsql]$ cd bin/
[postgres@blue bin]$ ./initdb -D ../data
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
The database cluster will be initialized with locale zh_CN.UTF-8.
The default database encoding has accordingly been set to UTF8.
initdb: could not find suitable text search configuration for locale zh_CN.UTF-8
The default text search configuration will be set to "simple".
creating directory ../data ... ok
。。。。。
Success. You can now start the database server using:
./postgres -D ../data
or
./pg_ctl -D ../data -l logfile start
初始化完成后,会在 /usr/local/pgsql/data目录生成数据库的文件,至此,软件安装完毕,数据库创建完毕。
5. 启动数据库[root@blue local]# cd pgsql/bin
[postgres@blue bin]$ ./pg_ctl -D ../data/ start
server starting
[postgres@blue bin]$ LOG: database system was shut down at 2012-04-17 15:15:07 CST
LOG: autovacuum launcher started
LOG: database system is ready to accept connections
启动后,可以发现PostgreSQL实例一共运行了5个进程
$ ps -ef | grep postgres
postgres 17572 1 0 16:41 pts/2 00:00:00 /opt/postgresql/bin/postgres
-D /opt/postgresql/data
postgres 17574 17572 0 16:41 ? 00:00:00 postgres: writer process
postgres 17575 17572 0 16:41 ? 00:00:00 postgres: wal writer process
postgres 17576 17572 0 16:41 ? 00:00:00 postgres: autovacuum launcher process
postgres 17577 17572 0 16:41 ? 00:00:00 postgres: stats collector process
postgres 18679 20791 0 16:47 pts/2 00:00:00 grep postgres
其中wal writer process是日志写进程。
6. 启动归档日志打开数据库目录下的postgresql.conf文件,进行如下配置
# - Settings -
wal_level = archive # minimal, archive, or hot_standby
# - Archiving -
archive_mode = on # allows archiving to be done
# (change requires restart)
archive_command = 'cp -i %p /mnt/server/archivedir/%f' # command to use to archive a logfile segment
archive_timeout = 60 # force a logfile segment switch after this
# number of seconds; 0 disables
注:archive_mode 需要设置,否则出现错误“FATAL: WAL archival (archive_mode=on) requires wal_level "archive" or "hot_standby"”。
然后放开用户权限:
[root@blue bin]$ cd /mnt/
[root@blue mnt]# mkdir server
[root@blue mnt]# chmod a+w server
[root@blue mnt]# cd server/
[root@blue server]# mkdir archivedir
[root@blue server]# chmod a+w archivedir/
然后重新启动数据库即可。
注:若不放开权限,会出错:
cp: 无法创建一般文件“/mnt/server/archivedir/000000010000000000000001”: 权限不够
LOG: archive command failed with exit code 1
DETAIL: The failed archive command was: cp -i pg_xlog/000000010000000000000001 /mnt/server/archivedir/000000010000000000000001
cp: 无法创建一般文件“/mnt/server/archivedir/000000010000000000000001”: 权限不够
LOG: archive command failed with exit code 1
DETAIL: The failed archive command was: cp -i pg_xlog/000000010000000000000001 /mnt/server/archivedir/000000010000000000000001
cp: 无法创建一般文件“/mnt/server/archivedir/000000010000000000000001”: 权限不够
LOG: archive command failed with exit code 1
DETAIL: The failed archive command was: cp -i pg_xlog/000000010000000000000001 /mnt/server/archivedir/000000010000000000000001
WARNING: transaction log file "000000010000000000000001" could not be archived: too many failures
7. 建立数据库$createdb mydb
PostgreSQL 会返回 “ CREATED DATABASE”的信息,表明数据库建立完成。
$psql mydb
进入交互 psql 工具,建立表:
CREATE TABLE mytable (
id varchar(20),
name varchar(30));
建立完成后,会得到一条 “CREATED” 的信息,表示建立成功。现在插入一条数据:
INSERT INTO mytable values('Author', 'Xu Yongjiu');
psql 返回 INSERT 18732 1,查询插入是否成功:
SELECT * FROM MYTABLE;
退出 psql ,用 \q 命令。
参考资料:
http://blog.csdn.net/opendba/article/details/5856312
http://blog.csdn.net/opendba/article/details/5856312
Postgresql备份: