postgresql教程
2025年12月2日小于 1 分钟约 282 字
PostgreSQL对比MySQL
- 支持丰富的数据类型
- 支持序列(Sequence)的概念
- 支持丰富的插件
- 一行数据会存储多个版本,数据有历史记录
安装
https://www.postgresql.org/download/linux/ubuntu/
# 添加PostgreSQL官方仓库
apt install -y postgresql-common ca-certificates
/usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
# 安装
sudo apt update
sudo apt install postgresql-18
# 配置
su postgres
cd /etc/postgresql/18/main
# 用户级别权限和连接配置
vim pg_hba.conf
host all all 0.0.0.0/0 scra-sha-256
# 服务级别的配置
vim postgresql.conf
listen_address = '*'
port 15432
systemctl enable postgresql
systemctl start postgresql
# 进入终端
su postgres
psql
\list # 查看数据库
\dn # 查看当前数据的架构和拥有者
\du; # 查看全部用户
\help alter # 查看alter命令文档
alter user postgres with password '123456'; # 给postgres用户设置密码
# 创建超级管理员
create user root with superuser password '123456'; # 创建一个root超级管理员用户
create database root; # 创建root数据库
# 创建普通用户(同时也可以给创建数据库权限)
create user demo with password '123456' CREATEDB;
create database demo_db owner demo;
\c demo_db; # 切换数据库
pg_default表空间的磁盘位置是/var/lib/postgresql/18/main/base