2025年9月3日
3 分钟阅读

postgresql 入门

postgresql的部署和权限

postgresql 入门

安装

apt install postgresql

连接

一. 切换用户
postgres 的默认账号是postgres 但是无法直接通过控制台登录

sudo -i -u postgres
pgsql

二. 修改登录方式

# 14是当前的版本号
# 修改 /etc/postgresql/14/main/pg_hba.conf
# 修改前
# local   all             postgres                peer
# 修改后
local   all             postgres                 trust

重启postgresql服务
此时可以直接登录

psql -U postgres
# 登录后设置密码
alter user postgres with password '你的密码';

修改/etc/postgresql/14/main/pg_hba.conf

local   all             postgres                peer

并保存。
此时可以通过密码登录

psql -U postgres -h localhost -d postgres

三. 创建新用户

postgres
create user xxx with pawword 'xxx'; create role;

创建数据库

一. 创建数据库

postgres
create database xx owner xxx;

二 赋予用户数据库权限

postgresql
GRANT ALL PRIVILEGES ON DATABASE xxx TO xx;
评论区 (0)
你的临时ID:
暂无评论,来发表第一条评论吧!