ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

访问GitLab的PostgreSQL数据库,查询、修改、替换等操作

2022-02-09 16:35:15  阅读:434  来源: 互联网

标签:GitLab PostgreSQL DEFAULT 数据库 gitlab production boolean gitlabhq public


1.登陆gitlab的安装服务查看配置文件

复制代码
cat /var/opt/gitlab/gitlab-rails/etc/database.yml 

production:
  adapter: postgresql
  encoding: unicode
  collation:
  database: gitlabhq_production  //数据库名
  pool: 10
  username: 'gitlab'  //用户名
  password:
  host: '/var/opt/gitlab/postgresql'  //主机
  port: 5432
  socket:
  sslmode:
  sslrootcert:
  sslca:
复制代码

查看/etc/passwd文件里边gitlab对应的系统用户

 

复制代码
[root@localhost ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
gitlab-www:x:496:493::/var/opt/gitlab/nginx:/bin/false
git:x:495:492::/var/opt/gitlab:/bin/sh
gitlab-redis:x:494:491::/var/opt/gitlab/redis:/bin/false
gitlab-psql:x:493:490::/var/opt/gitlab/postgresql:/bin/sh  //gitlab的postgresql用户
复制代码

 

2.根据上面的配置信息登陆postgresql数据库

复制代码
[root@localhost ~]# su - gitlab-psql     //登陆用户

-sh-4.1$ psql -h /var/opt/gitlab/postgresql -d gitlabhq_production   //连接到gitlabhq_production库

psql (9.2.18)
Type "help" for help.

gitlabhq_production=#  \h    //查看帮助命令

Available help:
ABORT CREATE FUNCTION DROP TABLE
ALTER AGGREGATE CREATE GROUP DROP TABLESPACE
ALTER COLLATION CREATE INDEX DROP TEXT SEARCH CONFIGURATION
ALTER CONVERSION CREATE LANGUAGE DROP TEXT SEARCH DICTIONARY
ALTER DATABASE CREATE OPERATOR DROP TEXT SEARCH PARSER
ALTER DEFAULT PRIVILEGES CREATE OPERATOR CLASS DROP TEXT SEARCH TEMPLATE
ALTER DOMAIN CREATE OPERATOR FAMILY DROP TRIGGER
ALTER EXTENSION CREATE ROLE DROP TYPE

……………………………………………………………………………………………………………………

 

gitlabhq_production-# \l     //查看数据库
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges 
---------------------+-------------+----------+-------------+-------------+---------------------------------
gitlabhq_production | gitlab | UTF8 | en_US.UTF-8 | en_US.UTF-8 | 
postgres | gitlab-psql | UTF8 | en_US.UTF-8 | en_US.UTF-8 | 
template0 | gitlab-psql | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/"gitlab-psql" +
| | | | | "gitlab-psql"=CTc/"gitlab-psql"
template1 | gitlab-psql | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/"gitlab-psql" +
| | | | | "gitlab-psql"=CTc/"gitlab-psql"
(4 rows)

gitlabhq_production-# \c gitlabhq_production //进入gitlabhq_production库 gitlabhq_production-# \dt //查看多表 List of relations Schema | Name | Type | Owner --------+--------------------------------------+-------+-------- public | abuse_reports | table | gitlab public | appearances | table | gitlab public | application_settings | table | gitlab public | audit_events | table | gitlab public | award_emoji | table | gitlab public | boards | table | gitlab public | broadcast_messages | table | gitlab …………………………………………………………………………………………………………………… gitlabhq_production-# \d abuse_reports //查看单表 Table "public.abuse_reports" Column | Type | Modifiers --------------+-----------------------------+------------------------------------------------------------ id | integer | not null default nextval('abuse_reports_id_seq'::regclass) reporter_id | integer | user_id | integer | message | text | created_at | timestamp without time zone | updated_at | timestamp without time zone | message_html | text | Indexes: "abuse_reports_pkey" PRIMARY KEY, btree (id) gitlabhq_production-# \di //查看索引 List of relations Schema | Name | Type | Owner | Table --------+-----------------------------------------------------------------+-------+--------+-------------------------------- ------ public | abuse_reports_pkey | index | gitlab | abuse_reports public | appearances_pkey | index | gitlab | appearances public | application_settings_pkey | index | gitlab | application_settings public | audit_events_pkey | index | gitlab | audit_events public | award_emoji_pkey | index | gitlab | award_emoji public | boards_pkey | index | gitlab | boards public | broadcast_messages_pkey | index | gitlab | broadcast_messages public | chat_names_pkey | index | gitlab | chat_names public | ci_application_settings_pkey | index | gitlab | ci_application_settings public | ci_builds_pkey | index | gitlab | ci_builds public | ci_commits_pkey | index | gitlab | ci_commits ……………………………………………………………………………………………………………………………………………… gitlabhq_production=# SELECT spcname FROM pg_tablespace; //查看所有表空间 spcname ------------ pg_default pg_global (2 rows) gitlabhq_production-# \q //退出psql -sh-4.1$ exit //退出登录用户 logout
复制代码

批量替换某个表字段的字符串

update tableA set field=replace(field,'value1','value2')

 

导出整个库:

pg_dump -h 127.0.0.1 -U testuser test >test.sql

导入整个库

psql -U testuser test < test.sql

注意:这里的testuser是postgres用户,test是数据库名称,而且,testuser需要有test的权限

 

 

这里笔者出现一个很大的坑,我是12的版本升级到13.12.15的。

创建项目时报错

 

经查是 

 gitlabhq_production  库中services 表缺少group_id字段导致的

CREATE TABLE public.services (
    id integer NOT NULL,
    type character varying,
    title character varying,
    project_id integer,
    created_at timestamp without time zone,
    updated_at timestamp without time zone,
    active boolean DEFAULT false NOT NULL,
    properties text,
    template boolean DEFAULT false,
    push_events boolean DEFAULT true,
    issues_events boolean DEFAULT true,
    merge_requests_events boolean DEFAULT true,
    tag_push_events boolean DEFAULT true,
    note_events boolean DEFAULT true NOT NULL,
    category character varying DEFAULT 'common'::character varying NOT NULL,
    "default" boolean DEFAULT false,
    wiki_page_events boolean DEFAULT true,
    pipeline_events boolean DEFAULT false NOT NULL,
    confidential_issues_events boolean DEFAULT true NOT NULL,
    commit_events boolean DEFAULT true NOT NULL,
    job_events boolean DEFAULT false NOT NULL,
    confidential_note_events boolean DEFAULT true,
    deployment_events boolean DEFAULT false NOT NULL,
    description character varying(500),
    comment_on_event_enabled boolean DEFAULT true NOT NULL,
    instance boolean DEFAULT false NOT NULL,
    comment_detail smallint,
    inherit_from_id bigint,
    alert_events boolean,
    group_id bigint
);

 解决方式

ALTER TABLE services add COLUMN group_id bigint;

 

查看表结构

# su - gitlab-psql
-sh-4.2$ pg_dump -s gitlabhq_production -t services

  

  

 

标签:GitLab,PostgreSQL,DEFAULT,数据库,gitlab,production,boolean,gitlabhq,public
来源: https://www.cnblogs.com/xzlive/p/15875560.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有