l
一,什么是postgresql
PostgreSQL是以加州大学伯克利分校计算机系开发的 POSTGRES 版本 4.2 为基础的对象关系型数据库管理系统(ORDBMS),简称pgsql,它支持大部分 SQL 标准并且提供了许多其他现代特性:复杂查询 外键 触发器 视图 事务完整性 多版本并发控制 同样,PostgreSQL 可以用许多方法扩展,比如, 通过增加新的:数据类型 函数 操作符 聚集函数 索引方法 过程语言 并且,因为许可证的灵活,任何人都可以以任何目的免费使用,修改,和分发 PostgreSQL, 不管是私用,商用,还是学术研究使用。
二,安装postgresql
查看复制打印?
[root@rudder ~]# yum search postgresql //先找一下,server版会有多个,决定选择装哪个
[root@rudder ~]# yum install postgresql postgresql-server
[root@rudder ~]# yum search postgresql //先找一下,server版会有多个,决定选择装哪个 [root@rudder ~]# yum install postgresql postgresql-server
三,配置postgresql
1,初始化数据库
a),错误用法
查看复制打印?
[root@rudder data]# service postgresql initdb //不管我用root用户,还是postgres都不行
Usage: /etc/init.d/postgresql {start|stop|status|restart|condrestart|condstop|reload|force-reload}
[root@rudder data]# service postgresql initdb //不管我用root用户,还是postgres都不行 Usage: /etc/init.d/postgresql {start|stop|status|restart|condrestart|condstop|reload|force-reload}
我在找资料的时候,发现有很多页面都用上面这样的用法,我就觉得很奇怪,service命令,还能这么用。一试果然不行。会不会是pgsql版本不同造成,service postgresql initdb不能用呢?
查看复制打印?
[root@rudder ~]# postgres --version //我的版本
postgres (PostgreSQL) 8.1.23
[root@rudder ~]# postgres --version //我的版本 postgres (PostgreSQL) 8.1.23
b),正确做法
查看复制打印?
[root@rudder data]# mkdir -p /var/lib/pgsql/data //创建pgsql的数据库目录
[root@rudder pgsql]# cd /var/lib/pgsql
[root@rudder pgsql]# chown postgres.postgres data //改变目录的所属用户用组
[root@rudder pgsql]# su postgres //切换到postgres,不然初始化不了
bash-3.2$ initdb -E UTF-8 -D /var/lib/pgsql/data --locale=en_US.UTF-8 -U postgres -W //初始化数据库
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 en_US.UTF-8.
fixing permissions on existing directory /var/lib/pgsql/data ... ok
creating directory /var/lib/pgsql/data/global ... ok
creating directory /var/lib/pgsql/data/pg_xlog ... ok
creating directory /var/lib/pgsql/data/pg_xlog/archive_status ... ok
creating directory /var/lib/pgsql/data/pg_clog ... ok
creating directory /var/lib/pgsql/data/pg_subtrans ... ok
creating directory /var/lib/pgsql/data/pg_twophase ... ok
creating directory /var/lib/pgsql/data/pg_multixact/members ... ok
creating directory /var/lib/pgsql/data/pg_multixact/offsets ... ok
creating directory /var/lib/pgsql/data/base ... ok
creating directory /var/lib/pgsql/data/base/1 ... ok
creating directory /var/lib/pgsql/data/pg_tblspc ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 1000
creating configuration files ... ok
creating template1 database in /var/lib/pgsql/data/base/1 ... ok
initializing pg_authid ... ok
Enter new superuser password: //超级管理员密码
Enter it again: //确认密码
initdb: removing contents of data directory "/var/lib/pgsql/data"
[root@rudder data]# mkdir -p /var/lib/pgsql/data //创建pgsql的数据库目录 [root@rudder pgsql]# cd /var/lib/pgsql [root@rudder pgsql]# chown postgres.postgres data //改变目录的所属用户用组 [root@rudder pgsql]# su postgres //切换到postgres,不然初始化不了 bash-3.2$ initdb -E UTF-8 -D /var/lib/pgsql/data --locale=en_US.UTF-8 -U postgres -W //初始化数据库 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 en_US.UTF-8. fixing permissions on existing directory /var/lib/pgsql/data ... ok creating directory /var/lib/pgsql/data/global ... ok creating directory /var/lib/pgsql/data/pg_xlog ... ok creating directory /var/lib/pgsql/data/pg_xlog/archive_status ... ok creating directory /var/lib/pgsql/data/pg_clog ... ok creating directory /var/lib/pgsql/data/pg_subtrans ... ok creating directory /var/lib/pgsql/data/pg_twophase ... ok creating directory /var/lib/pgsql/data/pg_multixact/members ... ok creating directory /var/lib/pgsql/data/pg_multixact/offsets ... ok creating directory /var/lib/pgsql/data/base ... ok creating directory /var/lib/pgsql/data/base/1 ... ok creating directory /var/lib/pgsql/data/pg_tblspc ... ok selecting default max_connections ... 100 selecting default shared_buffers ... 1000 creating configuration files ... ok creating template1 database in /var/lib/pgsql/data/base/1 ... ok initializing pg_authid ... ok Enter new superuser password: //超级管理员密码 Enter it again: //确认密码 initdb: removing contents of data directory "/var/lib/pgsql/data"
这样就初始化就好了。
2,修改/var/lib/pgsql/data/postgresql.conf
查看复制打印?
listen_addresses = '*' //监听所有ip的连接,默认是本机
port = 5432 //这个不开也行,默认就是5432端口
listen_addresses = '*' //监听所有ip的连接,默认是本机 port = 5432 //这个不开也行,默认就是5432端口
pgsql是我没有找到web版的管理工具,所以开发时肯定要用pgadmin远程连接的。所以权限是要放大,listen_addresses = '*' 全部放开,当然也可以,设置局域网的IP段。
3,修改/var/lib/pgsql/data/pg_hba.conf
查看复制打印?
# TYPE DATABASE USER CIDR-ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all 0.0.0.0/0 md5 //这一行我加的,所有IP和用户,密码对都可以连接
# IPv6 local connections:
host all all ::1/128 md5
# TYPE DATABASE USER CIDR-ADDRESS METHOD # "local" is for Unix domain socket connections only local all all trust # IPv4 local connections: host all all 127.0.0.1/32 md5 host all all 0.0.0.0/0 md5 //这一行我加的,所有IP和用户,密码对都可以连接 # IPv6 local connections: host all all ::1/128 md5
三,启动并查看
查看复制打印?
[root@rudder data]# /etc/init.d/postgresql start //启动
Initializing database: [ OK ]
Starting postgresql service: [ OK ]
[root@rudder data]# netstat -tpnl |grep 5432 //查看
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 27806/postmaster
[root@rudder data]# /etc/init.d/postgresql start //启动 Initializing database: [ OK ] Starting postgresql service: [ OK ] [root@rudder data]# netstat -tpnl |grep 5432 //查看 tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 27806/postmaster
四,用postgres用户登录,并改密码
查看复制打印?
[root@rudder data]# su postgres //切换用户
bash-3.2$ psql -U postgres //连接pgsql server
Welcome to psql 8.1.23, 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
postgres=# Alter USER postgres WITH PASSWORD '***密码**'; //添加密码
ALTER ROLE //出现这个才算成功,第一次操作没成功,pgadmin连不上
postgres-# \q //退出
[root@rudder data]# su postgres //切换用户 bash-3.2$ psql -U postgres //连接pgsql server Welcome to psql 8.1.23, 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 postgres=# Alter USER postgres WITH PASSWORD '***密码**'; //添加密码 ALTER ROLE //出现这个才算成功,第一次操作没成功,pgadmin连不上 postgres-# \q //退出
五,pgadmin客户端工具连接pgsql server
1,下载地址:http://www.postgresql.org/ftp/pgadmin3/release/v1.16.1/win32/
2,安装就不说了,傻瓜似操作,看一下连接图和连接成功图