postgresql命令

远程连接

psql -h IP地址 -p 端口  -U 数据库名
# 之后会要求输入数据库密码

查看安装位置

psql -U postgres -c 'SHOW config_file'

安装

CentOS7

sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# 客户端
sudo yum install -y postgresql12 
# 服务器
sudo yum install -y postgresql12-server

# 初始化
/usr/pgsql-12/bin/postgresql-12-setup initdb
systemctl enable --now postgresql-12

改配置

# 改密码
su - postgres 
psql -c "alter user postgres with password 'StrongPassword'" 

# 改配置
vim /var/lib/pgsql/12/data/pg_hba.conf

# Accept from anywhere
host all all 0.0.0.0/0 md5

# Accept from trusted subnet
host all all 192.168.18.0/24 md5


# 重启
systemctl restart postgresql-12

更改默认的schema

访问数据库

功能
命令

列举数据库

\l

选择数据库

\c 数据库名

查看所有表

\dt\dt+

查看表结构

\d 表名\d+ 表名

显示字符集

\encoding

退出psgl

\q

选取所有enums

\dT+

展示空值

\pset null 'NULL'

关闭换行

\pset pager off

查看空间:

DDL

索引重命名

导出

导入

导出表结构

清理空间

设置pg_xlog大小

修改 postgres.confwal 开头的东西

查看正在执行的sql

修改编辑模式

在脚本中自动输入密码

  1. 在~/目录下创建隐藏文件 .pgpass

  2. 文件内容:host:port:dbname:username:password

  3. bash_profileexport PGPASSFILE=~/.pgpass

  4. chmod 0600 ~/.pgpass

修改表结构

创建用户

重置自增ID

DML

列转行

联表更新

排序

提取时间差

字符串拼接

字符串替换

Last updated