极狐GitLab 正式推出面向 GitLab 老旧版本(12.x、13.x、14.x、15.x 等)免费用户的专业升级服务,点击查看详情

返回列表 发布新帖

数据库常用命令

699 0
发表于 2024-4-15 13:02:35 | 显示全部楼层 阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

×
# 数据库登录
## 本地登录
$ gitlab-psql
or
$ su - git
$ /opt/gitlab/embedded/bin/psql -h /var/opt/gitlab/postgresql -U gitlab -d gitlabhq_production 
## 远程登录
$ /opt/gitlab/embedded/bin/psql -h <ip> -p 5432 -U gitlab -d gitlabhq_production 

# 备份恢复
## 备份
$ su - gitlab-psql
$ /opt/gitlab/embedded/bin/pg_dump -h <ip> -p 5432 -d gitlabhq_production -U gitlab-psql --if-exists --clean -f database.sql -- 同 gitlab-backup
## 恢复
$ su - gitlab-psql
$ /opt/gitlab/embedded/bin/psql -h <ip> -p 5432 -U gitlab-psql -d gitlabhq_production -f database.sql

# 数据库操作
select pg_postmaster_start_time(); -- 数据库启动时间
select version(); -- 服务端版本

select pg_reload_conf(); -- 在线重载配置文件
select pg_conf_load_time(); -- 查看配置文件加载时间

select pg_terminate_backend(pid) from pg_stat_activity where datname=''; -- 结束连接到某个数据库的所有会话

select pg_is_in_recovery(); -- 判断主库(f)还是从库(t)
\x
select * from pg_stat_replication; -- 查看当前运行正常的流复制

select pg_create_physical_replication_slot('<slot-name>'); -- 创建物理复制槽
select pg_drop_replication_slot('<slot-name>'); -- 删除物理复制槽

vacuum full analyze; -- 清理数据库/回收空间,此命令会重新排序所有数据(操作期间数据库不可用)

select name,setting,unit from pg_settings where setting <> boot_val; -- 查看数据库修改过的所有参数

\? -- 查看所有快捷命令
\h -- 查看所有帮助命令
\l+ -- 查看所有数据库详细信息
\d+ <table-name> or \dt+ <table-name> -- 查看某张表详细信息
\dn -- 查看所有schema
\du -- 查看所有用户和角色

# 如果使用的RDS还可能用到以下命令
CREATE USER gitlab with encrypted password '<PASSWORD>'; -- 创建用户
CREATE DATABASE gitlabhq_production WITH OWNER = gitlab ENCODING = UTF8;
\c gitlabhq_production gitlab
CREATE EXTENSION pg_trgm;
CREATE EXTENSION btree_gist;
GRANT ALL PRIVILEGES ON SCHEMA public TO PUBLIC; -- 授予 public 使用权限
回复

使用道具 举报

意见建议

Email:forum@gitlab.cn
  • 关注公众号
  • 添加专业顾问
Copyright © 2001-2024 极狐GitLab论坛 版权所有 All Rights Reserved. 鄂ICP备2021008419号-1|鄂公网安备42018502006137号
关灯 快速发帖
扫一扫添加专业顾问
返回顶部
快速回复 返回顶部 返回列表