• PG14源码安装


    一、安装步骤

    1、设置保存安装包的目录

    yum install -y gcc gcc-c++
    yum install -y wget
    yum install -y readline-devel
    yum install -y zlib-devel
    yum install -y bison
    yum install -y flex
    yum install -y vim
    cd /usr/local/src
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2、开始下载源包

    wget https://ftp.postgresql.org/pub/source/v14.4/postgresql-14.4.tar.gz      (https://www.postgresql.org/ftp/source/ 这里是官网资源)
    
    • 1

    3、解压

    tar zxvf postgresql-14.4.tar.gz

    4、移动位置/usr/local/pgsql/postgresql-14.4

    mkdir -p /usr/local/pgsql
    mv postgresql-14.4 /usr/local/pgsql/postgresql-14.4
    
    • 1
    • 2

    6、进入移动后的目录

    cd /usr/local/pgsql/postgresql-14.4
    
    • 1

    7、配置选项生成Makefile,默认安装到目录/usr/local/pgsql/postgresql-14.4

    ./configure            (如果要安装到自定义目录,这里修改为  # ./configure --prefix= /usr/local/pgsql  红字部分是你的自定义路径)
    说明:./configure源代码安装的第一步,主要的作用是对即将安装的软件进行配置,检查当前的环境是否满足要安装软件的依赖关系
    
    • 1
    • 2

    8、echo 一下返回是否为0, 0表示无错误

    
    echo $?
    
    
    • 1
    • 2
    • 3

    9、编译

    make (编译后再echo $? 如果为0 就可以安装了)
    
    • 1

    10、安装

    make install
    
    • 1

    11、创建,添加postgres 用户到 postgres组

    groupadd -g3000 postgres                       (创建组:postgres )
    useradd -g3000 -G postgres -u3000 postgres     (命令解释:useradd -g 用户组 用户 )
    echo 'postgres' | passwd --stdin postgres      (设置postgres密码 )
    passwd
    mkdir -p /usr/local/pgsql/data          (创建一个data 目录)
    chown postgres /usr/local/pgsql/data    (改变data的权限为postgres用户)
    su - postgres                           (切换操作用户为 postgres)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    12、初始化数据库

    /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/
    (提示:初始化数据库的操作为: ./initdb -D /usr/local/pgsql/data initdb把用户指定的选项转换成对应的参数,通过外部程序调用的方式执行postgres程序。postgres程序在这种方式下将进入bootstrap模式创建数据集簇,并读取后端接口postgres.bki文件来创建模板数据库。)
    
    • 1
    • 2

    13、启动数据库

    /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l logfile start(启动数据库)
    /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ stop            (停止数据库)
    /usr/local/pgsql/bin/pg_ctl restart -D /usr/local/pgsql/data/ -m fast (重启数据库)
    
    • 1
    • 2
    • 3

    二、postgreSQL配置

    1、修改postgresql.conf,修改主要的配置参数、归档参数

    修改前可以先备份一下
    cd /usr/local/pgsql/data/
    cp postgresql.conf postgresql.conf_bak
    vim /usr/local/pgsql/data/postgresql.conf
    修改listen_addresses = '*'    前面的#号需要去掉(按/键输入listen_addresses可以快速查找该行。)默认参数是‘localhost‘ 只监听本机IP修改为’*‘后,可以让远端的其它设备访问 。另外没有vim 可以使用# yum install -y vim 安装vim文本编辑工具。
    wal_level='replica'
    archive_mode='on'
    archive_command ='cp %p /usr/local/pgsql/archive_wals/%f'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2、修改pg_hba.conf,修改数据库的访问权限

    cp /usr/local/pgsql/data/pg_hba.conf /usr/local/pgsql/data/pg_hba.conf_bak
    vim /usr/local/pgsql/data/pg_hba.conf
    添加以下,表示主机所有IP都可以访问。
    host            all             all               0.0.0.0/0           trust
    
    • 1
    • 2
    • 3
    • 4

    3、添加环境变量

    su -                            (输入密码后切换为root管理员)
    cd /home/postgres   (之前我们创建了一个postgres的用户所以这里有这个目录)
    vim .bash_profile   
    添加
    export PGHOME=/usr/local/pgsql
    export PGDATA=/usr/local/pgsql/data
    PATH=$PATH:$HOME/bin:$PGHOME/bin
    source .bash_profile (使修改生效)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4、设置开机自启动(chkconfig和systemctl二选一,建议systemctl方式)

    --chkconfig方式
    cd /usr/local/pgsql/postgresql-14.4/contrib/start-scripts (这里有个文件名叫linux的文件,它是linux系统的启动脚本)
    cp linux /etc/init.d/postgresql   (复制linux文件到/etc/init.d目录下,并更名postgresql)
    chmod +x /etc/init.d/postgresql
    vim linux /etc/init.d/postgresql
    查看
    prefix=/usr/local/pgsql    (安装程序的文件路径)
    PGDATA="/usr/local/pgsql/data" (数据存放目录)
    如果和自己的路径一致就不需要修改了,按ESC键 ,然后:q退出编辑。
    chkconfig --add postgresql (添加开机启动项目)
    chkconfig  (看下是否设置成功)
    
    --systemctl方式
    vim /etc/systemd/system/postgresql.service
    [Unit]
    Description=PostgreSQL database server
    After=network.target
    [Service]
    Type=forking
    User=postgres
    Group=postgres
    Environment=PGPORT=5432
    Environment=PGDATA=/usr/local/pgsql/data
    OOMScoreAdjust=-1000
    ExecStart=/usr/local/pgsql/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300
    ExecStop=/usr/local/pgsql/bin/pg_ctl stop -D ${PGDATA} -s -m fast
    ExecReload=/usr/local/pgsql/bin/pg_ctl reload -D ${PGDATA} -s
    TimeoutSec=300
    [Install]
    WantedBy=multi-user.target
    
    
    systemctl daemon-reload
    systemctl start postgresql.service
    systemctl enable postgresql.service
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    5、设置系统配置,开放默认的5432端口

    如果CentOS6 使用iptables执行以下步骤。
    vim /etc/sysconfig/iptables
    添加
    -A INPUT -p tcp -m tcp --dport 5432 -j ACCEPT    
    cd /etc/init.d/iptables restart (重启服务)
    如果CentOS7 有使用firewall防火墙执行以下步骤。
    firewall-cmd --zone=public --list-ports
    firewall-cmd --zone=public --add-port=5432/tcp --permanent  (添加5432端口)
    firewall-cmd --reload  (重启防火墙)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    6、启动服务

    --chkconfig方式
    service postgresql start
    --systemctl方式
    systemctl daemon-reload
    systemctl start postgresql
    systemctl enable postgresql
    systemctl stop postgresql
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    7、查看PostgreSQL服务

    ps -ef | grep postgres
    
    • 1

    8、设置默认密码

    PostgreSQL安装后会自动创建一个用户,名为postgres
    # su - postgres  (默认密码为空,切换到postgres用户)
    # psql -U postgres  
    postgres=#   ALTER USER postgres with encrypted password 'postgres'; (asd111密码自由设置)
    postgres=#   \q (可退出)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    三、数据库参数优化总结

    1. 共享缓冲区(Shared_buffers2. wal缓冲区 wal_buffers3. 有效缓存大小 effective_cache_size4. 工作内存(Work_mem)5. 维护工作内存(Maintainance_work_mem6. 检查点参数 checkpoint_timeout, checkpoint_completion_target7. 同步提交(Syncronous_commithttps://pgtune.leopard.in.ua/#/cat /proc/cpuinfo |grep ‘processor’|sort -u|wc -lmax_connectionswork_mem+shared_buffers+temp_buffers+maintenance_work_mem+操作系统所需内存KaTeX parse error: Expected 'EOF', got '#' at position 743: …es = '*' 前面的#̲号需要去掉(按/键输入list…PATH: H O M E / b i n : HOME/bin: HOME/bin:PGHOME/binsource .bash_profile (使修改生效)4、设置开机自启动cd /usr/local/pgsql/postgresql-14.4/contrib/start-scripts cp linux /etc/init.d/postgresql chmod +x /etc/init.d/postgresqlvim /etc/init.d/postgresql查看prefix=/usr/local/pgsql (安装程序的文件路径)PGDATA=“/usr/local/pgsql/data” (数据存放目录)如果和自己的路径一致就不需要修改了,按ESC键 ,然后:q退出编辑。chkconfig --add postgresql (添加开机启动项目)chkconfig (看下是否设置成功)5、设置系统配置,开放默认的5432端口如果CentOS6 使用iptables执行以下步骤。vim /etc/sysconfig/iptables添加-A INPUT -p tcp -m tcp --dport 5432 -j ACCEPT cd /etc/init.d/iptables restart (重启服务)如果CentOS7 有使用firewall防火墙执行以下步骤。firewall-cmd --zone=public --list-portsfirewall-cmd --zone=public --add-port=5432/tcp --permanent (添加5432端口)firewall-cmd --reload (重启防火墙)6、启动服务# service postgresql start7、查看PostgreSQL服务# ps -ef | grep postgres8、设置默认密码PostgreSQL安装后会自动创建一个用户,名为postgres# su - postgres (默认密码为空,切换到postgres用户)# psql -U postgres postgres=# ALTER USER postgres with encrypted password ‘postgres’; (asd111密码自由设置)postgres=# \q (可退出)三、数据库参数优化总结max_connections = 500shared_buffers = 194GBwork_mem = 256MBmaintenance_work_memeffective_cache_size = 240GBwal_buffers = 1GBcheckpoint_timeout = 55minmax_connections = 500 # (change requires restart)unix_socket_directories = ‘/tmp’ # comma-separated list of directories服务器侦听客户端应用程序连接的Unix域套接字的目录。shared_buffers = 194GB # 尽量用数据库管理内存,减少双重缓存,提高使用效率huge_pages = on # on, off, or try ,使用大页work_mem = 256MB # min 64kB , 减少外部文件排序的可能,提高效率maintenance_work_mem = 2GB # min 1MB , 加速建立索引autovacuum_work_mem = 2GB # min 1MB, or -1 to use maintenance_work_mem , 加速垃圾回收dynamic_shared_memory_type = posix # the default is the first optionvacuum_cost_delay = 0 # 0-100 milliseconds , 垃圾回收不妥协,极限压力下,减少膨胀可能性bgwriter_delay = 10ms # 10-10000ms between rounds , 刷shared buffer脏页的进程调度间隔,尽量高频调度,减少用户进程申请不到内存而需要主动刷脏页的可能(导致RT升高)。bgwriter_lru_maxpages = 1000 # 0-1000 max buffers written/round , 一次最多刷多少脏页bgwriter_lru_multiplier = 10.0 # 0-10.0 multipler on buffers scanned/round 一次扫描多少个块,上次刷出脏页数量的倍数effective_io_concurrency = 2 # 1-1000; 0 disables prefetching , 执行节点为bitmap heap scan时,预读的块数。从而wal_level = minimal # minimal, archive, hot_standby, or logical , 如果现实环境,建议开启归档。synchronous_commit = off # synchronization level; , 异步提交wal_sync_method = open_sync # the default is the first option , 因为没有standby,所以写xlog选择一个支持O_DIRECT的fsync方法。full_page_writes = off # recover from partial page writes , 生产中,如果有增量备份和归档,可以关闭,提高性能。wal_buffers = 1GB # min 32kB, -1 sets based on shared_buffers ,wal buffer大小,如果大量写wal buffer等待,则可以加大。wal_writer_delay = 10ms # 1-10000 milliseconds wal buffer调度间隔,和bg writer delay类似。commit_delay = 20 # range 0-100000, in microseconds ,分组提交的等待时间commit_siblings = 9 # range 1-1000 , 有多少个事务同时进入提交阶段时,就触发分组提交。checkpoint_timeout = 55min # range 30s-1h 时间控制的检查点间隔。max_wal_size = 320GB # 2个检查点之间最多允许产生多少个XLOG文件checkpoint_completion_target = 0.99 # checkpoint target duration, 0.0 - 1.0 ,平滑调度间隔,假设上一个检查点到现在这个检查点之间产生了100个XLOG,则这次检查点需要在产生100checkpoint_completion_target个XLOG文件的过程中完成。PG会根据这些值来调度平滑检查点。random_page_cost = 1.0 # same scale as above , 离散扫描的成本因子,本例使用的SSD IO能力足够好effective_cache_size = 240GB # 可用的OS CACHElog_destination = ‘csvlog’ # Valid values are combinations oflogging_collector = on # Enable capturing of stderr and csvloglog_truncate_on_rotation = on # If on, an existing log file with theupdate_process_title = offtrack_activities = offautovacuum = on # Enable autovacuum subprocess? 'on’autovacuum_max_workers = 4 # max number of autovacuum subprocesses ,允许同时有多少个垃圾回收工作进程。autovacuum_naptime = 6s # time between autovacuum runs , 自动垃圾回收探测进程的唤醒间隔autovacuum_vacuum_cost_delay = 0 # default vacuum cost delay for , 垃圾回收不妥协
  • 相关阅读:
    echo -e -n
    基于Servlet WebSocket MySQL实现的网络在线考试系统
    C++的动态内存分配
    Linux下yum源配置实战 2
    算法链与管道(上):建立管道
    Spring Boot 国际化 i18n
    从零开始之了解电机及其控制(10)空间矢量理论
    STC51单片机31——红外遥控收发代码
    canvas画布生成图片并下载
    python使用pandas中的read_csv函数读取csv数据为dataframe、使用isnull函数查看数据中是否包含缺失值
  • 原文地址:https://blog.csdn.net/qq961573863/article/details/127572084