一、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初始化配置内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论