postgresql搭建

来源:互联网 发布:淘宝指数数据查询 编辑:程序博客网 时间:2024/05/21 22:27

简介

  PostgreSQL标榜自己是世界上最先进的开源数据库。PostgreSQL的一些粉丝说它能与Oracle相媲美,而且没有那么昂贵的价格和傲慢的客服。它拥有很长的历史,最初是1985年在加利福尼亚大学伯克利分校开发的,作为Ingres数据库的后继。

  PostgreSQL是完全由社区驱动的开源项目,由全世界超过1000名贡献者所维护。它提供了单个完整功能的版本,而不像MySQL那样提供了多个不同的社区版、商业版与企业版。PostgreSQL基于自由的BSD/MIT许可,组织可以使用、复制、修改和重新分发代码,只需要提供一个版权声明即可。

环境说明

IP hostname 系统 数据库 10.10.10.60 test Centos6.5 PostgreSQL 9.2.15

安装

yum install postgresql postgresql-server postgresql-client#会自动创建postgres用户及postgres数据库[root@test ~]# postgres --versionpostgres (PostgreSQL) 9.2.15

配置

1.创建数据目录

#默认初始化,默认使用/var下生成数据目录service postgresql initdb#手动初始化,创建自定义的数据目录。在此我们使用自定义的数据目录mkdir /data/pgsql/datacd /datachown -R postgres.postgres pgsql

2.初始化数据库
默认情况下,数据目录为空,我们需要先对数据目录进行初始化才能生成配置文件及其他相关目录

initdb -E UTF-8 -D /data/pgsql/data --locale=en_US.UTF-8 -U postgres -W initdb: cannot be run as root#必须切换到postgres用户才能初始化[root@test ~]# su - postgres-bash-4.2$  initdb -E UTF-8 -D /data/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".The default text search configuration will be set to "english".fixing permissions on existing directory /data/pgsql/data ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 32MBcreating configuration files ... okcreating template1 database in /data/pgsql/data/base/1 ... okinitializing pg_authid ... okEnter new superuser password: Enter it again: setting password ... okinitializing dependencies ... okcreating system views ... okloading system objects' descriptions ... okcreating collations ... okcreating conversions ... okcreating dictionaries ... oksetting privileges on built-in objects ... okcreating information schema ... okloading PL/pgSQL server-side language ... okvacuuming database template1 ... okcopying template1 to template0 ... okcopying template1 to postgres ... okWARNING: enabling "trust" authentication for local connectionsYou can change this by editing pg_hba.conf or using the option -A, or--auth-local and --auth-host, the next time you run initdb.Success. You can now start the database server using:    postgres -D /data/pgsql/dataor    pg_ctl -D /data/pgsql/data -l logfile start

注意:初始化后postgres是唯一管理用户,默认是空密码;考虑到安全问题,通过initdb此我们设置了密码。
4.修改配置文件
客户端访问限制

vim pg_hba.conf #所有IP和用户,密码对都可以连接  host    all             all             0.0.0.0/0               md5

注:trust为无密码信任登录,只需输入ip和port即可登录;mds需要用户验证登录;ident为映射系统账户到pgsql访问账户。
例如:我们默认情况下使用root,但是必须切换到postgres管理员用户才能执行initdb或psql等命令,而如果我们将root映射成postgres管理员,即可不需要切换用户。

#在pg_hba.conf添加本地账户为identvim pg_hba.conflocal   all             all            ident map=map_root#在pg_ident.conf中添加映射,将本地root账户映射为pgsql管理员账户postgresvim pg_ident.conf# MAPNAME       SYSTEM-USERNAME         PG-USERNAMEmap_root        root            postgres#测试在系统账户root下,直接以postgres账户登录数据库;而默认下是不允许这样登录的root@test data]# psql -Upostgrespsql (9.2.15)Type "help" for help.postgres=# 

配置文件

vim /data/pgsql/data/postgresql.conf#监听所有ip连接listen_addresses = '*'#默认端口port = 5432#日志目录log_directory = '/data/pgsql/pg_log'

5.启动

service postgresql start注意:此时启动是默认initdb的pgsql,由于我们更改了data目录,因此应该按如下启动:su - postgres -c "pg_ctl start -D /data/pgsql/data"-bash-4.2$ netstat -ntlp|grep :5432(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.)tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      22327/postgres      tcp6       0      0 :::5432                 :::*                    LISTEN      22327/postgres        0      0 ::1:5432                :::*                    LISTEN      21805/postgres 

6.修改密码
如果我们想更改管理员密码,我们应如下操作

[root@test ~]# su - postgres -c "psql -U postgres"psql (9.2.15)Type "help" for help.postgres=# Alter USER postgres WITH PASSWORD '密码';ALTER ROLE        //出现这个才算成功或者postgres=# \password postgres

7.重新加载配置文件

pg_ctl reload -D /data/pgsql/data或者psql登录后select pg_reload_conf()

总结

本文只是对数据库的实用进行简单入门,订制psql、实用图形化管理工具pg_admin等还需要我们后续深入学习。

1 0
原创粉丝点击