侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

PostgreSQL安装、配置及简单使用方法

数据库  /  管理员 发布于 5年前   202

一、PostgreSQL简介

1、什么是PostgreSQL

PostgreSQL数据库是目前功能最强大的开源数据库,支持丰富的数据类型(如JSON何JSONB类型,数组类型)和自定义类型。而且它提供了丰富的接口,可以很容易地扩展它的功能,如可以在GiST框架下实现自己的索引类型等,它还支持使用C语言写自定义函数、触发器,也支持使用流行的语言写自定义函数,比如其中的PL/Perl提供了使用Perl语言写自定义函数的功能,当然还有PL/Python、PL/Tcl,等等。

2、PostgreSQL数据库的优势

PostgreSQL数据库是目前功能最强大的开源数据库,它是最接近工业标准SQL92的查询语言,并且正在实现新的功能已兼容最新的SQL标准:SQL2003.

稳定可靠:PostgreSQL是唯一能做到数据零丢失的开源数据库。有报道称国外的部分银行也在使用PostgreSQL。

开源省钱:PostgreSQL数据库是开源的、免费的,而且是BSD协议,在使用和二次开发上基本没有限制。

支持广泛:PostgreSQL数据库支持大量的主流开发语言,包括C、C++、Perl、Python、Java、Tcl,和PHP等。

PostgreSQL社区活跃:PostgreSQL基本上每三个月推出一个补丁版本,这意味着已知的BUG很快会被修复,有应用场景的需求也会及时得到响应。


二、PostgreSQL安装与配置

#安装前准备:

1、系统版本

[root@node1 ~]# cat /etc/redhat-releaseCentOS Linux release 7.2.1511 (Core)

2、yum安装(在官网上找到对应版本的yum源,之后安装到本地。

[root@node1 ~]# yum -y install pgdg-centos96-9.6-3.noarch.rpm #yum源安装[root@node1 ~]# yum -y install postgresql-server #安装postgreesql#安装生成的文件[root@node1 ~]# rpm -ql postgresql-server/etc/pam.d/postgresql/usr/bin/initdb/usr/bin/pg_basebackup/usr/bin/pg_controldata/usr/bin/pg_ctl/usr/bin/pg_receivexlog/usr/bin/pg_resetxlog/usr/bin/postgres/usr/bin/postgresql-check-db-dir/usr/bin/postgresql-setup/usr/bin/postmaster/usr/lib/systemd/system/postgresql.service/usr/lib/tmpfiles.d/postgresql.conf/var/lib/pgsql/var/lib/pgsql/.bash_profile/var/lib/pgsql/backups/var/lib/pgsql/data/var/run/postgresql、#启动postgresql#直接启动会报错:[root@node1 ~]# systemctl start postgresql.serviceJob for postgresql.service failed because the control process exited with error code. See "systemctl status postgresql.service" and "journalctl -xe" for details.#上面是提示数据库还没有初始化,所以我们先初始化一下postgresql-setup initdbInitializing database ... OK #提示初始化成功#重新启动Postgresql[root@node1 ~]# systemctl start postgresql.service[root@node1 ~]# netstat -tnlpProto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp  0  0 127.0.0.1:5432  0.0.0.0:*  LISTEN  1512/postgres  tcp6  0  0 ::1:5432    :::*  LISTEN  1512/postgres #查看运行状态[root@node1 ~]# systemctl status postgresql.service● postgresql.service - PostgreSQL database server Active: active (running) since Sat 2016-11-26 22:49:07 CST; 1min 33s ago#切换到操作系统下的“postgres”用户,登录数据库[root@node1 ~]# su - postgres-bash-4.2$ psqlpsql (9.2.15)Type "help" for help.postgres=# helpYou are using psql, the command-line interface to PostgreSQL.Type: \copyright for distribution terms  \h for help with SQL commands  \? for help with psql commands  \g or terminate with semicolon to execute query  \q to quit#到此为止,基本安装已经完成。

3、源码安装

#首先到官方网站下载源代码(https://www.postgresql.org/ftp/source/)

#开始编译安装[root@node1 soft]# tar xf postgresql-9.6.1.tar.bz2[root@node1 soft]# cd postgresql-9.6.1# yum -y groupinstall "Development tools" #开发包组# yum -y install perl-ExtUtils-Embed readline-devel zlib-devel python-devel #依赖包# ./configure --prefix=/usr/local/postgresql-9.6.1 --with-perl --with-python --with-blocksize=32 --with-wal-blocksize=64 --with-wal-segsize=64# make && make install#安装后的配置[root@node1 postgresql-9.6.1]# cat /etc/profile.d/postgresql.shexport PATH=$PATH:/usr/local/pgsql/binexport PGDATA=/data/pgdata[root@node1 postgresql-9.6.1]# source /etc/profile.d/postgresql.sh[root@node1 postgresql-9.6.1]# echo "/usr/local/pgsql/lib" > /etc/ld.so.conf.d/pgsql.conf[root@node1 postgresql-9.6.1]# ldconfig#创建数据库目录并初始化数据库[root@node1 postgresql-9.6.1]# mkdir /data/pgdata/[root@node1 postgresql-9.6.1]# chown -R postgres.postgres /data/pgdata/[root@node1 postgresql-9.6.1]# su - postgres-bash-4.2$ initdbThe database cluster will be initialized with locale "en_US.UTF-8".The default database encoding has accordingly been set to "UTF8".The default text search configuration will be set to "english".fixing permissions on existing directory /data/pgdata ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting dynamic shared memory implementation ... posixcreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... oksyncing data to disk ... okSuccess. You can now start the database server using: pg_ctl -D /data/pgdata -l logfile start#安装contrib目录下的工具# cd postgresql-9.6.1/contrib/# make# make install#启动和停止数据库# pg_ctl start -D $PGDATA #PGDATA是pgsql的数据目录# pg_ctl stop -D $PGDATA [-m SHUTDOWN-MODE]其中-m是制定数据库的停止方法,有以下三种smart:等所有的连接中止后,关闭数据库。如果客户端不中止,则无法关闭数据库。fast:快速关闭数据库,断开客户端的连接,让已有的事务回滚,然后正常关闭数据库。immediate:立即关闭数据库,相当于数据库进程立即停止,直接退出,下次启动数据库需要进行修复。

4、PostgreSQL的简单配置

在数据目录下编辑postgresql.conf文件,找到如下内容:#listen_addresses = 'localhost'   # what IP address(es) to listen on;#port = 5432       # (change requires restart)listen_addresses表示要监听的地址,要让网络上的主机登录这台数据库,需要把这个地址改成"*",或者0.0.0.0。port表示监听的端口,可以不更改,修改这两个参数后,需要重启才能生效。#与数据库Log相关的参数logging_collector = on #日志的收集,on表示打开log_directory = 'pg_log' #定义日志的收集目录日志的切换和是否选择覆盖则可以使用如下几种方案方案一:每天生产一个新的日志文件log_filename = ‘postgresql-%Y-%m-%d_%H%M%S.log'log_truncate_on_rotation = offlog_rotation_age = 1dlog_rotation_size = 0方案二:每当日志写满一定的大小(如10MB空间),则切换一个日志log_filename = ‘postgresql-%Y-%m-%d_%H%M%S.log'log_truncate_on_rotation = offlog_rotation_age = 0log_rotation_size = 10M方案三:只保留7天的日志,进行循环覆盖 log_filename = ‘postgresql-%a.log'log_truncate_on_rotation = offlog_rotation_age = 1dlog_rotation_size = 0

5、内存参数的设置

shared_buffers:共享内存的大小,主要用于共享数据块。

#shared_buffers默认值为32MB,如果有足够的内存,可以把这个参数改得大一些,这样数据库就可以缓存更多的数据库,当读取数据时,就可以从共享内存中读,而不需要再从文件上去读取。

work_mem:单个SQL执行时,排序、hash join所使用的内存,SQL运行完后,内存就释放了,把这个值设大一些,会让排序操作快一些。


三、SQL语法入门

1、SQL语句语法简介

(1)、语句的分类(SQL命令一般分为DDL、DML、DQL几类)

DDL:Data Definition Language的缩写,即数据定义语言,主要用于创建、删除,以及修改表、索引等数据库对象语言。

DML:Data Manipulation Language的简称,即数据操纵语言,主要用于插入、更新、删除数据,所以也分为INSERT、UPDATE、DELETE三种语句。

DQL:数据库查询语句,基本及时SELECT查询命令,用于数据查询。


  • 上一条:
    PostgreSQL之分区表(partitioning)
    下一条:
    PostgreSQL分区表(partitioning)应用实例详解
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客