kk Blog —— 通用基础


date [-d @int|str] [+%s|"+%F %T"]
netstat -ltunp
sar -n DEV 1

CentOS安装PostgreSQL

https://blog.csdn.net/u012190388/article/details/128025382

https://www.yisu.com/ask/99033014.html

Centos下安装postgreSQL

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 安装PostgreSQL
sudo yum install postgresql-server

# 初始化数据库
sudo postgresql-setup initdb

# 启动PostgreSQL服务
sudo systemctl start postgresql
sudo systemctl enable postgresql

# 设置数据库密码: 设置数据库用户postgres的密码:
sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD 'your_password';"

# 重启PostgreSQL服务
sudo systemctl restart postgresql

常用操作命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 切换到postgres用户
su postgres

# 切换SQL模式
psql

# 修改密码
alter user postgres with password 'postgres123';

# 创建test用户
create user test with password 'test';

# 授权
grant all privileges on database mydb to test;

# 退出
\q
1
2
3
4
5
6
7
8
9
10
11
# 创建数据库mydb
create database mydb;

# 查看所有数据库
\l

# 切换到mydb数据库
\c mydb

# 删除
drop database mydb ;
1
2
3
4
5
# 查看所有表
\d

# 查看表结构
\d tb

数据库备份与还原

实际工作中会对数据库进行备份和还原,备份主要有三种格式

.bak 即压缩的二进制

.sql 即明文存储

.tar 即tarball压缩格式

数据库备份分单数据库备份,使用 pg_dump 命令;所有数据库备份,使用 pg_dumpall 命令。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 切换到postgres用户
su postgres

# 单数据库备份-导出到当前目录
pg_dump mydb > mydb.bak

# 所有数据库备份,
pg_dumpall > backup.bak

# 整个数据库备份到指定位置
pg_dump -f /tmp/mydb.bak mydb

# 备份postgres数据库中business_order表
pg_dump -U postgres -f /tmp/mydb.sql -t business_order postgres

# 数据库恢复-直接恢复,注意先新增空数据库
psql -f /tmp/mydb.bak mydb

# 数据库恢复
pg_restore -U postgres -d business_order /temp/mydb.bak

切换模式(schema)

要在 PostgreSQL 中切换模式(schema),您可以使用 SET search_path 命令。模式是 PostgreSQL 数据库中用于组织和分隔对象的命名空间。

以下是如何在 psql 命令行客户端中切换模式的步骤:

  1. 打开 psql 客户端并连接到您的数据库。

  2. 使用以下命令查看当前搜索路径(search path):

1
SHOW search_path;

这将显示当前搜索路径,即数据库在查找对象时搜索的模式列表。

  1. 如果要切换到不同的模式,可以使用 SET search_path 命令。例如,要切换到名为 new_schema 的模式,可以执行:
1
SET search_path TO new_schema;

如果要切换到多个模式,可以将它们以逗号分隔列出。例如:

1
SET search_path TO schema1, schema2, schema3;
  1. 确认您已经成功切换到新的模式。可以再次使用 SHOW search_path; 命令验证。

通过这些步骤,您可以在 PostgreSQL 中轻松切换模式。

medicine, mimic

« gps WGS84与GCJ02经纬度坐标转换 wx.getLocation MIMIC III v1.4 数据入库 »