host DATABASE USER ADDRESS METHOD [OPTIONS]
# METHOD can be "trust", "reject", "md5", "password", "scram-sha-256",
# "gss", "sspi", "ident", "peer", "pam", "ldap", "radius" or "cert".
# Note that "password" sends passwords in clear text; "md5" or
# "scram-sha-256" are preferred since they send encrypted passwords.
postgres=# select version();
version
-----------------------------------------------------------------------------------------------------------
PostgreSQL 9.6.19 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
(1 row)
postgres=# create user user1 unencrypted password '123456';
CREATE ROLE
postgres=# select usename,passwd from pg_shadow;
usename | passwd
----------+--------
postgres |
user1 | 123456
(2 rows)
postgres=# select version();
version
---------------------------------------------------------------------------------------------------------
PostgreSQL 12.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
(1 row)
postgres=# create user user1;
CREATE ROLE
postgres=# create user user2 UNENCRYPTED PASSWORD '123';
ERROR: UNENCRYPTED PASSWORD is no longer supported
LINE 1: create user user2 UNENCRYPTED PASSWORD '123';
^
HINT: Remove UNENCRYPTED to store the password in encrypted form instead.
postgres=# create user user2 PASSWORD '123';
CREATE ROLE
postgres=# select usename,passwd from pg_shadow;
usename | passwd
----------+-------------------------------------
postgres |
user1 |
user2 | md5245a2b356234ce1ea772e164e596f395
(3 rows)
-bash-4.2$ psql
psql (12.4)
Type "help" for help.
postgres=# \c postgres user2
You are now connected to database "postgres" as user "user2".
postgres=>
postgres=> select * from pg_shadow ;
ERROR: permission denied for view pg_shadow
密码的加密方式如下:md5(xxxusername)
postgres=# select 'md5'||md5('123user2') as mypasswd,passwd,usename from pg_shadow where usename = 'user2';
mypasswd | passwd | usename
-------------------------------------+-------------------------------------+---------
md5245a2b356234ce1ea772e164e596f395 | md5245a2b356234ce1ea772e164e596f395 | user2
(1 row)
-bash-4.2$ cat postgres_data/postgresql.conf | grep listen_address
#listen_addresses = 'localhost' # what IP address(es) to listen on;
-bash-4.2$ psql -h 172.16.0.131 -p 5432
psql: error: could not connect to server: could not connect to server: Connection refused
Is the server running on host "172.16.0.131" and accepting
TCP/IP connections on port 5432?
-bash-4.2$ psql -h localhost -p 5432
psql (12.4)
Type "help" for help.
postgres=#


Specifies the TCP/IP address(es) on which the server is to listen for connections from client applications. The value takes the form of a comma-separated list of host names and/or numeric IP addresses. The special entry * corresponds to all available IP interfaces. The entry 0.0.0.0 allows listening for all IPv4 addresses and :: allows listening for all IPv6 addresses. If the list is empty, the server does not listen on any IP interface at all, in which case only Unix-domain sockets can be used to connect to it. The default value is localhost, which allows only local TCP/IP “loopback” connections to be made. While client authentication (Chapter 20) allows fine-grained control over who can access the server, listen_addresses controls which interfaces accept connection attempts, which can help prevent repeated malicious connection requests on insecure network interfaces. This parameter can only be set at server start.


-bash-4.2$ cat 12/data/postgresql.conf | grep listen_address
listen_addresses = '172.16.0.33' # what IP address(es) to listen on;
-bash-4.2$ ifconfig
eth0: flags=4163 mtu 1500
inet 172.16.0.131 netmask 255.255.255.0 broadcast 172.16.0.255
inet6 fe80::5054:ff:fe38:acc2 prefixlen 64 scopeid 0x20
ether 52:54:00:38:ac:c2 txqueuelen 1000 (Ethernet)
RX packets 5317565458 bytes 401090966314 (373.5 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5996981980 bytes 445432122403 (414.8 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1 (Local Loopback)
RX packets 129692682 bytes 35954878615 (33.4 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 129692682 bytes 35954878615 (33.4 GiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
-bash-4.2$ pg_ctl -D 12/data/ restart
waiting for server to shut down.... done
server stopped
waiting for server to start....2020-09-05 17:20:44.004 CST [6177] LOG: starting PostgreSQL 12.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
2020-09-05 17:20:44.004 CST [6177] LOG: could not bind IPv4 address "172.16.0.33": Cannot assign requested address
2020-09-05 17:20:44.004 CST [6177] HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2020-09-05 17:20:44.004 CST [6177] WARNING: could not create listen socket for "172.16.0.33"
2020-09-05 17:20:44.004 CST [6177] FATAL: could not create any TCP/IP sockets
2020-09-05 17:20:44.004 CST [6177] LOG: database system is shut down
stopped waiting
pg_ctl: could not start server
Examine the log output.
改成正常的地址,可以正常启动:
-bash-4.2$ cat 12/data/postgresql.conf | grep listen_address
listen_addresses = '172.16.0.131' # what IP address(es) to listen on;
-bash-4.2$ pg_ctl -D 12/data/ start
waiting for server to start....2020-09-05 17:23:25.350 CST [6590] LOG: starting PostgreSQL 12.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
2020-09-05 17:23:25.350 CST [6590] LOG: listening on IPv4 address "172.16.0.131", port 5432
2020-09-05 17:23:25.355 CST [6590] LOG: listening on Unix socket "/var/lib/pgsql/.s.PGSQL.5432"
2020-09-05 17:23:25.363 CST [6590] LOG: redirecting log output to logging collector process
2020-09-05 17:23:25.363 CST [6590] HINT: Future log output will appear in directory "log".
done
server started
-bash-4.2$ cat 12/data/postgresql.conf |grep listen_address
listen_addresses = '' # what IP address(es) to listen on;
-bash-4.2$ pg_ctl -D 12/data/ restart
waiting for server to shut down.... done
server stopped
waiting for server to start....2020-09-05 17:33:18.660 CST [8088] LOG: starting PostgreSQL 12.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
2020-09-05 17:33:18.663 CST [8088] LOG: listening on Unix socket "/var/lib/pgsql/.s.PGSQL.5432"
2020-09-05 17:33:18.673 CST [8088] LOG: redirecting log output to logging collector process
2020-09-05 17:33:18.673 CST [8088] HINT: Future log output will appear in directory "log".
done
server started
-bash-4.2$ netstat -nutlp | 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.)
-bash-4.2$ cat 12/data/postgresql.conf | grep unix
#unix_socket_directories = '/var/run/postgresql, tmp' # comma-separated list of directories
#unix_socket_group = '' # (change requires restart)
#unix_socket_permissions = 0777 # begin with 0 to use octal notation
-bash-4.2$ pg_ctl -D 12/data/ start
waiting for server to start....2020-09-05 16:33:20.731 CST [31528] LOG: starting PostgreSQL 12.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
-- 在ipv6的环回地址上监听
2020-09-05 16:33:20.731 CST [31528] LOG: listening on IPv6 address "::1", port 5432
-- 在ipv4的环回地址上监听
2020-09-05 16:33:20.731 CST [31528] LOG: listening on IPv4 address "127.0.0.1", port 5432
-- unix domain socket,分别位于/var/run/postgresql/.s.PGSQL.5432和/tmp/.s.PGSQL.5432
2020-09-05 16:33:20.734 CST [31528] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-09-05 16:33:20.738 CST [31528] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2020-09-05 16:33:20.743 CST [31528] LOG: redirecting log output to logging collector process
2020-09-05 16:33:20.743 CST [31528] HINT: Future log output will appear in directory "log".
done
server started
-bash-4.2$ ll var/run/postgresql/.s.PGSQL.5432
srwxrwxrwx 1 postgres postgres 0 Sep 5 16:33 var/run/postgresql/.s.PGSQL.5432
-bash-4.2$ mv var/run/postgresql/.s.PGSQL.5432 var/run/postgresql/.s.PGSQL.5432.bak
-bash-4.2$ psql
psql: error: could not connect to server: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
-bash-4.2$ mv var/run/postgresql/.s.PGSQL.5432.bak var/run/postgresql/.s.PGSQL.5432
-bash-4.2$ psql
psql (12.4)
Type "help" for help.
postgres=#
-bash-4.2$ psql -h localhost
psql (12.4)
Type "help" for help.
postgres=#
-bash-4.2$ netstat -anpo | grep psql
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp6 0 0 ::1:36628 ::1:5432 ESTABLISHED 1986/psql keepalive (7203.73/0/0)
-bash-4.2$ pg_ctl -D 12/data/ start
waiting for server to start....2020-09-05 16:56:07.806 CST [2541] LOG: starting PostgreSQL 12.4 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
2020-09-05 16:56:07.806 CST [2541] LOG: listening on IPv6 address "::1", port 5432
2020-09-05 16:56:07.806 CST [2541] LOG: listening on IPv4 address "127.0.0.1", port 5432
2020-09-05 16:56:07.812 CST [2541] LOG: listening on Unix socket "/var/lib/pgsql/.s.PGSQL.5432"
2020-09-05 16:56:07.819 CST [2541] LOG: redirecting log output to logging collector process
2020-09-05 16:56:07.819 CST [2541] HINT: Future log output will appear in directory "log".
done
server started
-bash-4.2$ netstat -a | grep PGSQ
unix 2 [ ACC ] STREAM LISTENING 1785021871 /var/lib/pgsql/.s.PGSQL.5432
-bash-4.2$ psql
psql: error: could not connect to server: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
-bash-4.2$ psql -h /var/lib/pgsql/
psql (12.4)
Type "help" for help.
postgres=# show unix_socket_directories;
unix_socket_directories
-------------------------
/var/lib/pgsql
(1 row)
# TYPE DATABASE USER 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 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
host all all 172.16.0.0/16 password
日常使用中,严格把控pg_shadow视图中的内容,非必要不去查,被抓一下包,很容易抓到你的密码,当然可以通过通信加密的方式来进一步保障安全
严禁使用password的认证方式,因为密码是明文传输的,大多数时候基于md5的认证方式就够用了
设定用户的密码有效期,定期修改密码,create user xxx vaild until xxx
通过listen_address也可以进一步限制,pg_hba.conf是针对client的访问方式,而listen_addresses是控制postgres server 具体使用哪个(IPV4/IPV6/BOTH)网络接口(interface)进行监听连接请求,对于网络接口而言,这可以有效阻止对大量恶意重复的连接
I Love PG
关于我们
中国开源软件推进联盟PostgreSQL分会(简称:中国PG分会)于2017年成立,由国内多家PostgreSQL生态企业所共同发起,业务上接受工信部中国电子信息产业发展研究院指导。中国PG分会是一个非盈利行业协会组织。我们致力于在中国构建PostgreSQL产业生态,推动PostgreSQL产学研用发展。
技术文章精彩回顾 PostgreSQL学习的九层宝塔 PostgreSQL职业发展与学习攻略 2019,年度数据库舍 PostgreSQL 其谁? Postgres是最好的开源软件 PostgreSQL是世界上最好的数据库 从Oracle迁移到PostgreSQL的十大理由 从“非主流”到“潮流”,开源早已值得拥有 PG活动精彩回顾 创建PG全球生态!PostgresConf.CN2019大会盛大召开 首站起航!2019“让PG‘象’前行”上海站成功举行 走进蓉城丨2019“让PG‘象’前行”成都站成功举行 中国PG象牙塔计划发布,首批合作高校授牌仪式在天津举行 群英论道聚北京,共话PostgreSQL 相聚巴厘岛| PG Conf.Asia 2019 DAY0、DAY1简报 相知巴厘岛| PG Conf.Asia 2019 DAY2简报 独家|硅谷Postgres大会简报 直播回顾 | Bruce Momjian:原生分布式将在PG 14版本发布 PG培训认证精彩回顾 中国首批PGCA认证考试圆满结束,203位考生成功获得认证! 中国第二批PGCA认证考试圆满结束,115位考生喜获认证! 重要通知:三方共建,中国PostgreSQL认证权威升级! 近500人参与!首次PGCE中级、第三批次PGCA初级认证考试落幕! 2020年首批 | 中国PostgreSQL初级认证考试圆满结束 一分耕耘一分收获,第五批次PostgreSQL认证考试成绩公布