PG数据库安装​timescale数据库以及备份配置

Posted 李石岩

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PG数据库安装​timescale数据库以及备份配置相关的知识,希望对你有一定的参考价值。

1.PG数据库安装

系统版本

数据库版本PG14.4


1.1 依赖包安装

yum -y install libicu libicu-devel libxslt perl-libs python-psycopg2 expect

rpm -ivh dwz-0.11-3.el7.x86_64.rpm

rpm -ivh perl-srpm-macros-1-8.el7.noarch.rpm

rpm -ivh python-srpm-macros-3-34.el7.noarch.rpm

rpm -ivh python-rpm-macros-3-34.el7.noarch.rpm

rpm -ivh redhat-rpm-config-9.1.0-88.el7.centos.noarch.rpm

rpm -ivh tk-8.5.13-6.el7.x86_64.rpm

yum -y localinstall python3*

rpm -ivh ncurses-devel-5.9-14.20130511.el7_4.x86_64.rpm

rpm -ivh libedit-devel-3.0-12.20121213cvs.el7.x86_64.rpm

yum -y localinstall llvm5.0-*


---> 软件包 llvm5.0.x86_64.0.5.0.1-7.el7 将被 安装

---> 软件包 llvm5.0-devel.x86_64.0.5.0.1-7.el7 将被 安装

---> 软件包 llvm5.0-libs.x86_64.0.5.0.1-7.el7 将被 安装

rpm -ivh kernel-headers-3.10.0-1160.71.1.el7.x86_64.rpm

rpm -ivh kernel-headers-3.10.0-1160.71.1.el7.x86_64.rpm

yum -y localinstall glibc*

--> 正在检查事务

---> 软件包 glibc.x86_64.0.2.17-317.el7 将被 升级

---> 软件包 glibc.x86_64.0.2.17-326.el7_9 将被 更新

---> 软件包 glibc-common.x86_64.0.2.17-317.el7 将被 升级

---> 软件包 glibc-common.x86_64.0.2.17-326.el7_9 将被 更新

---> 软件包 glibc-devel.x86_64.0.2.17-326.el7_9 将被 安装

---> 软件包 glibc-headers.x86_64.0.2.17-326.el7_9 将被 安装


yum -y localinstall devtoolset-7*

正在解决依赖关系

--> 正在检查事务

---> 软件包 devtoolset-7-binutils.x86_64.0.2.28-11.el7 将被 安装

---> 软件包 devtoolset-7-gcc.x86_64.0.7.3.1-5.16.el7 将被 安装

---> 软件包 devtoolset-7-gcc-c++.x86_64.0.7.3.1-5.16.el7 将被 安装

---> 软件包 devtoolset-7-libstdc++-devel.x86_64.0.7.3.1-5.16.el7 将被 安装

---> 软件包 devtoolset-7-runtime.x86_64.0.7.1-4.el7 将被 安装



rpm -ivh llvm-toolset-7-runtime-5.0.1-4.el7.x86_64.rpm

rpm -ivh llvm5.0-5.0.1-7.el7.x86_64.rpm

rpm -ivh llvm5.0-libs-5.0.1-7.el7.x86_64.rpm

rpm -ivh llvm5.0-devel-5.0.1-7.el7.x86_64.rpm

rpm -ivh llvm-toolset-7-compiler-rt-5.0.1-2.el7.x86_64.rpm

rpm -ivh llvm-toolset-7-libomp-5.0.1-2.el7.x86_64.rpm



rpm -ivh centos-release-scl-rh-2-3.el7.centos.noarch.rpm

rpm -ivh llvm-toolset-7-llvm-libs-5.0.1-8.el7.x86_64.rpm

rpm -ivh llvm-toolset-7-llvm-libs-5.0.1-8.el7.x86_64.rpm

rpm -ivh llvm-toolset-7-clang-libs-5.0.1-4.el7.x86_64.rpm

rpm -ivh llvm-toolset-7-clang-5.0.1-4.el7.x86_64.rpm


1.2数据库安装

rpm -ivh postgresql14-libs-14.4-1PGDG.rhel7.x86_64

rpm -ivh postgresql14-libs-14.4-1PGDG.rhel7.x86_64.rpm

rpm -ivh postgresql14-server-14.4-1PGDG.rhel7.x86_64.rpm

rpm -ivh postgresql14-contrib-14.4-1PGDG.rhel7.x86_64.rpm

rpm -ivh postgresql14-devel-14.4-1PGDG.rhel7.x86_64.rpm

rpm -ivh timescaledb_14-2.7.0-1.rhel7.x86_64.rpm


1.3 设置环境变量

Su - postgres

[postgres@timescale ~]-> cat .bash_profile

[ -f /etc/profile ] && source /etc/profile

PGDATA=/var/lib/pgsql/14/data

export PGDATA

# If you want to customize your settings,

# Use the file below. This is not overridden

# by the RPMS.

[ -f /var/lib/pgsql/.pgsql_profile ] && source /var/lib/pgsql/.pgsql_profile


export PS1="[$USER@`/bin/hostname -s` \\W]-> "

export PGPORT=5432

export PGDATA=/zdata/pg/pgmain

export LANG=en_US.utf8

export PGHOME=/usr/pgsql-14

export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH

export DATE=`date +"%Y%m%d%H%M"`

export PATH=$PGHOME/bin:$PATH

export MANPATH=$PGHOME/share/man:$MANPATH

export PGHOST=$PGDATA

export PGUSER=postgres

export PGDATABASE=postgres

alias rm=rm -i

alias ll=ls -lh

alias vi=vim

[postgres@timescale ~]->



1.4 初始化数据库

postgres用户

mkdir -p /zdata/dbbak/archivewal

chown -R postgres:postgres /zdata/dbbak/archivewal

initdb -D $PGDATA -U postgres -E UTF8 --lc-collate=C --lc-ctype=en_US.UTF8 -X /zdata/dbbak/archivewal -k


注意-X /zdata/dbbak/archivewal 这个归档不配置,我们后续再配置,初始化用下面的配置。


initdb -D $PGDATA -U postgres -E UTF8 --lc-collate=C --lc-ctype=en_US.UTF8 -k


Root用户修改权限

[root@timescale usr]# chown -R postgres.postgres /usr/pgsql-14/


1.5 创建systemctl服务启动

[root@timescale xxx]# systemctl enable postgresql-14.service

Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-14.service to /usr/lib/systemd/system/postgresql-14.service.

[root@timescale xxx]# vim /usr/lib/systemd/system/postgresql-14.service

[root@timescale xxx]# cat /usr/lib/systemd/system/postgresql-14.service

注意我们要修改

Environment=PGDATA=/var/lib/pgsql/14/data/

修改为

#Environment=PGDATA=/var/lib/pgsql/14/data/

Environment=PGDATA=/zdata/pg/pgmain/

然后

[root@timescale xxx]# systemctl daemon-reload


[root@timescale xxx]# systemctl start postgresql-14.service


1.6 pg_hba.conf

[postgres@timescale pgmain]-> egrep -v ^[[:space:]]|^$|^# pg_hba.conf

local all all trust

host all all 127.0.0.1/32 trust

host all all 0.0.0.0/0 md5

host all all ::1/128 trust

local replication all trust

host replication all 127.0.0.1/32 trust

host replication all ::1/128 trust

[postgres@timescale pgmain]->


1.7 postgresql.conf配置

shared_preload_libraries = timescaledb,pg_stat_statements这个必须有

其他参数按照数据库标准参数配置

2.用户安装

CREATE USER timescaler WITH PASSWORD timescaler login;

Create database timescaledb;

GRANT ALL PRIVILEGES ON DATABASE timescaledb TO timescaler;

GRANT ALL PRIVILEGES ON all tables in schema public TO timescaler;

timeseries=​​​# create extension timescaledb;                            # 方式一,添加 TimescaleDB 插件            ​​

timeseries=​​# CREATE EXTENSION IF NOT EXISTS timescaledb CASCADE;      # 方式二,使用 TimescaleDB 扩展数据库

PG数据库安装​timescale数据库以及备份配置_bash

3.安装go环境

[root@timescale ~]# tar -C /usr/local -xzf go1.18.4.linux-amd64.tar.gz

[root@timescale ~]# vim /etc/profile

[root@timescale ~]# tail -1 /etc/profile

export PATH=$PATH:/usr/local/go/bin

[root@timescale ~]# source /etc/profile


4.安装timescaledb-parallel-copy

su - postgres

# 临时启用Go Modules 功能

$ export GO111MODULE=on

# 临时设置 环境变量

$ export GOPROXY=https://goproxy.io,direct

# 还可以设置不走 proxy 的私有仓库或组,多个用逗号相隔

$ export GOPRIVATE=git.mycompany.com,github.com/my/private

go install github.com/timescale/timescaledb-parallel-copy/cmd/timescaledb-parallel-copy@latest


[postgres@timescale ~]-> cd go/bin/

[postgres@timescale bin]-> ls

timescaledb-parallel-copy

[postgres@timescale bin]->


设置环境变量:

export PATH=$PATH:/var/lib/pgsql/go/bin


注意:如果是离线安装的话,需要安装工具包,然后再进行安装。

GitHub - timescale/timescaledb-toolkit: Extension for more hyperfunctions, fully compatible with TimescaleDB and PostgreSQL

以上是关于PG数据库安装​timescale数据库以及备份配置的主要内容,如果未能解决你的问题,请参考以下文章

pg数据库的备份和恢复以及sql脚本错误的解决方法

TimescaleDB 简单试用

PostgreSQL 实现定时任务的 4 种方法

备份pgsql数据库提示pgserver的版本和pg_dump的版本不一致

PG运维篇--逻辑备份和物理备份实战

postgresql数据库备份命令