PostgreSQL数据库管理-第一章安装与配置

PostgreSQL数据库管理

第一章安装与配置

概述

PostgreSQL是一个功能非常强大的、源代码开放的客户/服务器关系型数据库管理系统(RDBMS)。支持丰富的数据类型(如JSON和JSONB类型,数组类型)和自定义类型。PostgreSQL内存页面的默认大小是8kB。

 

PostgreSQL有以下主要特性:

1良好支持SQL语言,支持ACID、关联完整性、数据库事务、Unicode多国语言。

2高并发设计,读和写互不阻塞

3 支持大量类型的数据库模型:关系型,文档型(如JSON和JSONB类型,数组类型),Key/value类型。

1 安装PostgreSQL

    1. linux下安装PostgreSQL的rpm包

1 安装存储库RPM:

Yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

2 安装服务器RPM包

yum list postgresql11-server.x86_64

 3 安装客户端软件包:

yum list postgresql11.x86_64 

4 初始化数据库并启用自动启动:

/ usr / pgsql-11 / bin / postgresql-11-setup initdb
systemctl enable postgresql-11
systemctl start postgresql-11

 

1.2 linux下安装PostgreSQL的源代码包

1 系统环境建立

gcc, bison, gcc-c++, readline, readline-devel, zlib, zlib-devel

yum install *gcc* -y

[root@Redhat7 ~]# yum install *bison* -y

[root@Redhat7 ~]# yum install *readline* -y

[root@Redhat7 ~]# yum install *zlib* -y  

2 与linux系统优化

ulimit 功   能:控制shell程序的资源

在Linux下面部署应用的时候,有时候会遇上Socket/File: Can’t open so many files的问题;这个值也会影响服务器的最大并发数,其实Linux是有文件句柄限制的,而且Linux默认不是很高,一般都是1024,生产服务器用其实很容易就达到这个数量。下面说的是,如何通过正解配置来改正这个系统默认值。

/etc/security/limits.conf

soft nproc :单个用户可用的最大进程数量(超过会警告);

hard nproc:单个用户可用的最大进程数量(超过会报错);

soft nofile  :可打开的文件描述符的最大数(超过会警告);

hard nofile :可打开的文件描述符的最大数(超过会报错);

# End of f11e

* soft  nof1le  1024000

* hard  nofile  1024000

* soft  nproc  1024000

* hard  nproc 1024000

 

[root@Redhat7 ~]# cat /etc/security/limits.d/20-nproc.conf

# Default limit for number of user's processes to prevent

# accidental fork bombs.

# See rhbz #432903 for reasoning.

 

*          soft    nproc     4096

root       soft    nproc     unlimited

修改成

 

*  soft nproc  1024000

*  hard nproc  1024000

root       soft    nproc     unlimited

 

 

 

3 创建postgres用户,并创建安装目录

[root@Redhat7 ~]# useradd postgres

[root@Redhat7 ~]# mkdir /opt/pgsql11.4   创建PG家目录

[root@Redhat7 ~]# mkdir /pgdb           创建PG数据目录

[root@Redhat7 ~]# chown -R postgres:postgres /opt/pgsql11.4/

[root@Redhat7 ~]# chown -R postgres:postgres /pgdb

 

4 安装PostgreSQL

[root@Redhat7 pgsql11.4]# tar -zxvf postgresql-11.4.tar.gz

[root@Redhat7 postgresql-11.4]# ./configure --prefix=/opt/pgsql11.4

make

make install

PostgreSQL installation complete.

prefix= PREFIX                   --安装路径最常修改

口with- blocksize= BLOCKSIZE        --数据库blocksize ,缺省8KB

OLAP场景下可以适当增加这个值到32kB,以提高OLAP的性能,但在OLTP场景下建议使用8kB默认值。

口with-segsize=SEGSIZE            --表文件的段尺寸,缺省1GB

口with-llvm                      --使用基于JIT的Ivm编译

口--with-wal-blocksize=BLOCKSIZE: 指定WAL文件的块大小,默认是8kB。

口--with-wal-segsize=SEGSIZE: 指定单个WAL文件的大小,默认是16MB。

5 配置环境变量

比较全的

vi /home/postgres/.bash_profile

添加内容:

export PGPORT=8432

export PGHOME=/usr/pgsql-11

export PGDATA=/mnt/db1/pgdata/pgsql

export PATH=$PGHOME/bin:$PATH

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

export LANG=en_US.UTF-8

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

export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH

export PGHOST=$PGDATA

export PGUSER=postgres

可以使用的

PGHOME=/opt/pgsql11.4

export PGHOME

PGDATA=/pgdb

export PGDATA

export PATH=$PGHOME/bin:$PATH

export PATH

6 初始化数据库

使用initdb命 令初始化数据库簇

一-D : database cluster的主目录

--E :数据库字符编码

--wal-segsize : WAL日志文件尺寸

- -U :指定超级用户

 

initdb -D /pgdb/

[postgres@RHCE7 ~]$ initdb -D /pgdb/

The files belonging to this database system will be owned by user "postgres".

This user must also own the server process.

 

The database cluster will be initialized with locale "zh_CN.UTF-8".

The default database encoding has accordingly been set to "UTF8".

initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"

The default text search configuration will be set to "simple".

 

Data page checksums are disabled.

 

fixing permissions on existing directory /pgdb ... ok

creating subdirectories ... ok

selecting default max_connections ... 100

selecting default shared_buffers ... 128MB

selecting default timezone ... PRC

selecting dynamic shared memory implementation ... posix

creating configuration files ... ok

running bootstrap script ... ok

performing post-bootstrap initialization ... ok

syncing data to disk ... ok

 

WARNING: enabling "trust" authentication for local connections

You can change this by editing pg_hba.conf or using the option -A, or

--auth-local and --auth-host, the next time you run initdb.

 

Success. You can now start the database server using:

 

    pg_ctl -D /pgdb/ -l logfile start

 

[postgres@RHCE7 ~]$   pg_ctl -D /pgdb/ -l logfile start

waiting for server to start.... done

server started

7 开机自动启动

 

[root@RHCE7 ~]# vim  /usr/lib/systemd/system/postgresql-11.service

 

[Unit]

Description=PostgreSQL database server

After=network.target

 

[Service]

Type=forking

 

User=postgres

Group=postgres

 

# Port number for server to listen on

Environment=PGPORT=5432

 

# Location of database directory

Environment=PGDATA=/pgdb/

 

# Where to send early-startup messages from the server (before the logging

# options of postgresql.conf take effect)

# This is normally controlled by the global default set by systemd

# StandardOutput=syslog

 

# Disable OOM kill on the postmaster

OOMScoreAdjust=-1000

 

#ExecStartPre=/opt/pgsql11.4/bin/postgresql-check-db-dir ${PGDATA}

ExecStart=/opt/pgsql11.4/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300

ExecStop=/opt/pgsql11.4/bin/pg_ctl stop -D ${PGDATA} -s -m fast

ExecReload=/opt/pgsql11.4/bin/pg_ctl reload -D ${PGDATA} -s

 

# Give a reasonable amount of time for the server to start up/shut down

TimeoutSec=300

 

[Install]

WantedBy=multi-user.target

 

 

[root@RHCE7 ~]# systemctl enable /usr/lib/systemd/system/postgresql-11.service

Init 6 重启测试

 

[root@RHCE7 ~]# systemctl status postgresql-11.service

● postgresql-11.service - PostgreSQL database server

   Loaded: loaded (/usr/lib/systemd/system/postgresql-11.service; enabled; vendor preset: disabled)

   Active: active (running) since 日 2019-09-15 00:37:04 CST; 3min 47s ago

  Process: 936 ExecStart=/opt/pgsql11.4/bin/pg_ctl start -D ${PGDATA} -s -o -p ${PGPORT} -w -t 300 (code=exited, status=0/SUCCESS)

1.3PostgreSQL实现远程访问(linux版)

linux版的postgresql默认无法直接远程访问其数据库因此,需要修改postgreSQL数据库配置来实现远程访问。

具体操作如下:在postgresql.conf下面修改

在最后添加用户参数:

listen_address = ‘*’,注意不要被注释掉

启用密码验证

# password_encryption = md5  修改为 password_encryption = md5

 

修改pg_hba.conf文件的内容:

可访问的用户ip段

在文件末尾加入:host  all  all  0.0.0.0/0  trust

也可以host  all  all  all  trust

 

重启postgreSQL数据库:启动成功后,再在远程连接

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值