当前位置: 代码网 > it编程>数据库>MsSqlserver > PostgreSQL初始化配置的实现小结

PostgreSQL初始化配置的实现小结

2025年11月06日 MsSqlserver 我要评论
一、postgresql安装dnf install -y postgresql-server# 初始化pgsql/usr/bin/postgresql-setup --initdb# 启用pgsqls

一、postgresql安装

dnf install -y postgresql-server
# 初始化pgsql
/usr/bin/postgresql-setup --initdb
# 启用pgsql
systemctl enable postgresql.service
systemctl start postgresql.service

二、postgresql配置远程访问

1、编辑/var/lib/pgsql/16/data/postgresql.conf(通过默认仓库安装路径是:/var/lib/pgsql/data/postgresql.conf),取消注释并修改:

listen_addresses = '*'       # 允许所有ip访问
port = 5432                  # 默认端口

2、编辑/var/lib/pgsql/16/data/pg_hba.conf(通过默认仓库安装路径是:/var/lib/pgsql/data/pg_hba.conf),按需选择以下配置

# local行仅针对unix域套接字连接,不涉及网络
# host行针对tcp/ip连接
# 允许本地socket连接访问数据库
local  all     all                      md5
# 允许本地tcp连接(127.0.0.1)使用md5:
host   all     all      127.0.0.1/32    md5
# 允许所有ip访问数据库
host   all     all      0.0.0.0/0       md5
# 允许某网段访问数据库:
host   all     all      192.168.1.0/24  md5
# 允许admin用户通过127.0.0.1访问数据库
host   all     admin    127.0.0.1/32    md5
# 允许admin用户通过127.0.0.1访问访问testdb库
host   testdb  admin    127.0.0.1/32    md5

3、重启postgresql

systemctl restart postgresql
service postgresql restart

三、postgresql认证方式

1、使用md5/password认证
远程连接认证必需使用md5/password方式,本地tcp/ip连接使用md5或scram-sha-256(scram-sha-256:一般在较新版本中使用,需要客户端库也支持scram,且pg_hba.conf与pgcrypto支持),需要修改pg_hba.conf(pg_hba.conf文件位置:/var/lib/pgsql/data/pg_hba.conf或/etc/postgresql/xx/main/pg_hba.conf)

2、使用ident/peer认证
ident/peer验证通常在本地连接时通过操作系统用户与数据库用户的映射来工作,如果没有正确映射也会失败。确保你在操作系统是以同名用户运行(系统中也需要有admin用户),并且pg_hba.conf中的local/host行匹配该使用者。四、postgresql常用操作

# 进入 psql
sudo -u postgres psql
# 创建数据库
create database testdb;
# 创建用户:
create user admin with password 'admin123';
# 授予用户访问testdb数据库权限:
grant all privileges on database testdb to admin;
# 查看所有数据库
\l
# 切换到testdb库
\c testdb
# 查看数据库里的表
\dt
# 查看当前数据库名称
select current_database();
# 退出
\q
# 远程登陆postgresql
psql -h 127.0.0.1 -u admin -d testdb -w admin123

到此这篇关于postgresql初始化配置的实现小结的文章就介绍到这了,更多相关postgresql初始化配置内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com