您的位置:首页 > 运维架构 > Linux

在虚拟机(centos)配置postgresql数据库(2) - 配置篇

2014-02-25 20:09 627 查看
鼓捣了几天 pg_hba.conf文件,终于配置成功了。。

# PostgreSQL Client Authentication Configuration File
# ===================================================
#
# Refer to the "Client Authentication" section in the PostgreSQL
# documentation for a complete description of this file.  A short
# synopsis follows.
#
# This file controls: which hosts are allowed to connect, how clients
# are authenticated, which PostgreSQL user names they can use, which
# databases they can access.  Records take one of these forms:
#
# local      DATABASE  USER  METHOD  [OPTIONS]
# host       DATABASE  USER  ADDRESS  METHOD  [OPTIONS]
# hostssl    DATABASE  USER  ADDRESS  METHOD  [OPTIONS]
# hostnossl  DATABASE  USER  ADDRESS  METHOD  [OPTIONS]
#
# (The uppercase items must be replaced by actual values.)
#
# The first field is the connection type: "local" is a Unix-domain
# socket, "host" is either a plain or SSL-encrypted TCP/IP socket,
# "hostssl" is an SSL-encrypted TCP/IP socket, and "hostnossl" is a
# plain TCP/IP socket.
#
# DATABASE can be "all", "sameuser", "samerole", "replication", a
# database name, or a comma-separated list thereof. The "all"
# keyword does not match "replication". Access to replication
# must be enabled in a separate record (see example below).
#
# USER can be "all", a user name, a group name prefixed with "+", or a
# comma-separated list thereof.  In both the DATABASE and USER fields
# you can also write a file name prefixed with "@" to include names
# from a separate file.
#
# ADDRESS specifies the set of hosts the record matches.  It can be a
# host name, or it is made up of an IP address and a CIDR mask that is
# an integer (between 0 and 32 (IPv4) or 128 (IPv6) inclusive) that
# specifies the number of significant bits in the mask.  A host name
# that starts with a dot (.) matches a suffix of the actual host name.
# Alternatively, you can write an IP address and netmask in separate
# columns to specify the set of hosts.  Instead of a CIDR-address, you
# can write "samehost" to match any of the server's own IP addresses,
# or "samenet" to match any address in any subnet that the server is
# directly connected to.
#
# METHOD can be "trust", "reject", "md5", "password", "gss", "sspi",
# "krb5", "ident", "peer", "pam", "ldap", "radius" or "cert".  Note that
# "password" sends passwords in clear text; "md5" is preferred since
# it sends encrypted passwords.
#
# OPTIONS are a set of options for the authentication in the format
# NAME=VALUE.  The available options depend on the different
# authentication methods -- refer to the "Client Authentication"
# section in the documentation for a list of which options are
# available for which authentication methods.
#
# Database and user names containing spaces, commas, quotes and other
# special characters must be quoted.  Quoting one of the keywords
# "all", "sameuser", "samerole" or "replication" makes the name lose
# its special character, and just match a database or username with
# that name.
#
# This file is read on server startup and when the postmaster receives
# a SIGHUP signal.  If you edit the file on a running system, you have
# to SIGHUP the postmaster for the changes to take effect.  You can
# use "pg_ctl reload" to do that.

# Put your actual configuration here
# ----------------------------------
#
# If you want to allow non-local connections, you need to add more
# "host" records.  In that case you will also need to make PostgreSQL
# listen on a non-local interface via the listen_addresses
# configuration parameter, or via the -i or -h command line switches.

# CAUTION: Configuring the system for local "trust" authentication
# allows any local user to connect as any PostgreSQL user, including
# the database superuser.  If you do not trust all your local users,
# use another authentication method.
# "local" is for Unix domain socket connections only
local    all      all                 ident
#  IPv4 local connections:
host     all      all   192.168.1.0/24 md5
# IPv6 local connections:
host     all      all   ::1/128       md5


补充一下:后面的24及128都是子网掩码的1的个数譬如:255.255.255.255 二进制共有32个1,而255.255.255.0二进制共有24个1,,明白了吗?

/article/7027636.html


PostgreSQL pg_hba.conf 文件简析

作者:高张远瞩(HiLoveS)
博客:http://www.cnblogs.com/hiloves/
转载请保留该信息
 
最近试用PostgreSQL 9.04,将pg_hba.conf配置的一些心得分享。pg_hba.conf是客户端认证配置文件,定义如何认证客户端。
下面是常用的pg_hba.conf配置:

 
TYPE定义了多种连接PostgreSQL的方式,分别是:“local”使用本地unix套接字,“host”使用TCP/IP连接(包括SSL和非SSL),“host”结合“IPv4地址”使用IPv4方式,结合“IPv6地址”则使用IPv6方式,“hostssl”只能使用SSL TCP/IP连接,“hostnossl”不能使用SSL TCP/IP连接。
DATABASE指定哪个数据库,多个数据库,库名间以逗号分隔。“all”只有在没有其他的符合条目时才代表“所有”,如果有其他的符合条目则代表“除了该条之外的”,因为“all”的优先级最低。如下例:

这两条都是指定local访问方式,因为前一条指定了特定的数据库db1,所以后一条的all代表的是除了db1之外的数据库,同理用户的all也是这个道理。
USER指定哪个数据库用户(PostgreSQL正规的叫法是角色,role)。多个用户以逗号分隔。
CIDR-ADDRESS项local方式不必填写,该项可以是IPv4地址或IPv6地址,可以定义某台主机或某个网段。
METHOD指定如何处理客户端的认证。常用的有ident,md5,password,trust,reject。
ident是Linux下PostgreSQL默认的local认证方式,凡是能正确登录服务器的操作系统用户(注:不是数据库用户)就能使用本用户映射的数据库用户不需密码登录数据库。用户映射文件为pg_ident.conf,这个文件记录着与操作系统用户匹配的数据库用户,如果某操作系统用户在本文件中没有映射用户,则默认的映射数据库用户与操作系统用户同名。比如,服务器上有名为user1的操作系统用户,同时数据库上也有同名的数据库用户,user1登录操作系统后可以直接输入psql,以user1数据库用户身份登录数据库且不需密码。很多初学者都会遇到psql
-U username登录数据库却出现“username ident 认证失败”的错误,明明数据库用户已经createuser。原因就在于此,使用了ident认证方式,却没有同名的操作系统用户或没有相应的映射用户。解决方案:1、在pg_ident.conf中添加映射用户;2、改变认证方式。
md5是常用的密码认证方式,如果你不使用ident,最好使用md5。密码是以md5形式传送给数据库,较安全,且不需建立同名的操作系统用户。
password是以明文密码传送给数据库,建议不要在生产环境中使用。
trust是只要知道数据库用户名就不需要密码或ident就能登录,建议不要在生产环境中使用。
reject是拒绝认证。
 
本地使用psql登录数据库,是以unix套接字的方式,附合local方式。
使用PGAdmin3或php登录数据库,不论是否本地均是以TCP/IP方式,附合host方式。如果是本地(数据库地址localhost),CIDR-ADDRESS则为127.0.0.1/32。
 
例:
1、允许本地使用PGAdmin3登录数据库,数据库地址localhost,用户user1,数据库user1db:

2、允许10.1.1.0~10.1.1.255网段登录数据库:

3、信任192.168.1.10登录数据库:

 
pg_hba.conf修改后,使用pg_ctl reload重新读取pg_hba.conf文件,如果pg_ctl找不到数据库,则用-D /.../pgsql/data/ 指定数据库目录,或export PGDATA=/.../pgsql/data/ 导入环境变量。
 
另:PostgreSQL默认只监听本地端口,用netstat -tuln只会看到“tcp 127.0.0.1:5432 LISTEN”。修改postgresql.conf中的listen_address=*,监听所有端口,这样远程才能通过TCP/IP登录数据库,用netstat -tuln会看到“tcp 0.0.0.0:5432 LISTEN”。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: