佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > PostgreSQL安裝、配置及簡單使用方法

PostgreSQL安裝、配置及簡單使用方法

熱門標簽:網(wǎng)站排名優(yōu)化 地方門戶網(wǎng)站 Linux服務(wù)器 服務(wù)外包 呼叫中心市場需求 AI電銷 百度競價排名 鐵路電話系統(tǒng)

一、PostgreSQL簡介

1、什么是PostgreSQL

PostgreSQL數(shù)據(jù)庫是目前功能最強大的開源數(shù)據(jù)庫,支持豐富的數(shù)據(jù)類型(如JSON何JSONB類型,數(shù)組類型)和自定義類型。而且它提供了豐富的接口,可以很容易地擴展它的功能,如可以在GiST框架下實現(xiàn)自己的索引類型等,它還支持使用C語言寫自定義函數(shù)、觸發(fā)器,也支持使用流行的語言寫自定義函數(shù),比如其中的PL/Perl提供了使用Perl語言寫自定義函數(shù)的功能,當然還有PL/Python、PL/Tcl,等等。

2、PostgreSQL數(shù)據(jù)庫的優(yōu)勢

PostgreSQL數(shù)據(jù)庫是目前功能最強大的開源數(shù)據(jù)庫,它是最接近工業(yè)標準SQL92的查詢語言,并且正在實現(xiàn)新的功能已兼容最新的SQL標準:SQL2003.

穩(wěn)定可靠:PostgreSQL是唯一能做到數(shù)據(jù)零丟失的開源數(shù)據(jù)庫。有報道稱國外的部分銀行也在使用PostgreSQL。

開源省錢:PostgreSQL數(shù)據(jù)庫是開源的、免費的,而且是BSD協(xié)議,在使用和二次開發(fā)上基本沒有限制。

支持廣泛:PostgreSQL數(shù)據(jù)庫支持大量的主流開發(fā)語言,包括C、C++、Perl、Python、Java、Tcl,和PHP等。

PostgreSQL社區(qū)活躍:PostgreSQL基本上每三個月推出一個補丁版本,這意味著已知的BUG很快會被修復,有應(yīng)用場景的需求也會及時得到響應(yīng)。


二、PostgreSQL安裝與配置

#安裝前準備:

1、系統(tǒng)版本

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

2、yum安裝(在官網(wǎng)上找到對應(yīng)版本的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.service
Job for postgresql.service failed because the control process exited with error code. See "systemctl status postgresql.service" and "journalctl -xe" for details.
#上面是提示數(shù)據(jù)庫還沒有初始化,所以我們先初始化一下
postgresql-setup initdb
Initializing database ... OK #提示初始化成功
#重新啟動Postgresql
[root@node1 ~]# systemctl start postgresql.service
[root@node1 ~]# netstat -tnlp
Proto 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 
#查看運行狀態(tài)
[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
#切換到操作系統(tǒng)下的“postgres”用戶,登錄數(shù)據(jù)庫
[root@node1 ~]# su - postgres
-bash-4.2$ psql
psql (9.2.15)
Type "help" for help.
postgres=# help
You 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
#到此為止,基本安裝已經(jīng)完成。

3、源碼安裝

#首先到官方網(wǎng)站下載源代碼(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" #開發(fā)包組
# 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.sh
export PATH=$PATH:/usr/local/pgsql/bin
export 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
#創(chuàng)建數(shù)據(jù)庫目錄并初始化數(shù)據(jù)庫
[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$ initdb
The 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 ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
Success. 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
#啟動和停止數(shù)據(jù)庫
# pg_ctl start -D $PGDATA #PGDATA是pgsql的數(shù)據(jù)目錄
# pg_ctl stop -D $PGDATA [-m SHUTDOWN-MODE]
其中-m是制定數(shù)據(jù)庫的停止方法,有以下三種
smart:等所有的連接中止后,關(guān)閉數(shù)據(jù)庫。如果客戶端不中止,則無法關(guān)閉數(shù)據(jù)庫。
fast:快速關(guān)閉數(shù)據(jù)庫,斷開客戶端的連接,讓已有的事務(wù)回滾,然后正常關(guān)閉數(shù)據(jù)庫。
immediate:立即關(guān)閉數(shù)據(jù)庫,相當于數(shù)據(jù)庫進程立即停止,直接退出,下次啟動數(shù)據(jù)庫需要進行修復。

4、PostgreSQL的簡單配置

在數(shù)據(jù)目錄下編輯postgresql.conf文件,找到如下內(nèi)容:
#listen_addresses = 'localhost'   # what IP address(es) to listen on;
#port = 5432       # (change requires restart)
listen_addresses表示要監(jiān)聽的地址,要讓網(wǎng)絡(luò)上的主機登錄這臺數(shù)據(jù)庫,需要把這個地址改成"*",或者0.0.0.0。
port表示監(jiān)聽的端口,可以不更改,修改這兩個參數(shù)后,需要重啟才能生效。
#與數(shù)據(jù)庫Log相關(guān)的參數(shù)
logging_collector = on #日志的收集,on表示打開
log_directory = 'pg_log' #定義日志的收集目錄
日志的切換和是否選擇覆蓋則可以使用如下幾種方案
方案一:每天生產(chǎn)一個新的日志文件
log_filename = ‘postgresql-%Y-%m-%d_%H%M%S.log'
log_truncate_on_rotation = off
log_rotation_age = 1d
log_rotation_size = 0
方案二:每當日志寫滿一定的大小(如10MB空間),則切換一個日志
log_filename = ‘postgresql-%Y-%m-%d_%H%M%S.log'
log_truncate_on_rotation = off
log_rotation_age = 0
log_rotation_size = 10M
方案三:只保留7天的日志,進行循環(huán)覆蓋 
log_filename = ‘postgresql-%a.log'
log_truncate_on_rotation = off
log_rotation_age = 1d
log_rotation_size = 0

5、內(nèi)存參數(shù)的設(shè)置

shared_buffers:共享內(nèi)存的大小,主要用于共享數(shù)據(jù)塊。

#shared_buffers默認值為32MB,如果有足夠的內(nèi)存,可以把這個參數(shù)改得大一些,這樣數(shù)據(jù)庫就可以緩存更多的數(shù)據(jù)庫,當讀取數(shù)據(jù)時,就可以從共享內(nèi)存中讀,而不需要再從文件上去讀取。

work_mem:單個SQL執(zhí)行時,排序、hash join所使用的內(nèi)存,SQL運行完后,內(nèi)存就釋放了,把這個值設(shè)大一些,會讓排序操作快一些。


三、SQL語法入門

1、SQL語句語法簡介

(1)、語句的分類(SQL命令一般分為DDL、DML、DQL幾類)

DDL:Data Definition Language的縮寫,即數(shù)據(jù)定義語言,主要用于創(chuàng)建、刪除,以及修改表、索引等數(shù)據(jù)庫對象語言。

DML:Data Manipulation Language的簡稱,即數(shù)據(jù)操縱語言,主要用于插入、更新、刪除數(shù)據(jù),所以也分為INSERT、UPDATE、DELETE三種語句。

DQL:數(shù)據(jù)庫查詢語句,基本及時SELECT查詢命令,用于數(shù)據(jù)查詢。

您可能感興趣的文章:
  • 詳解CentOS7下PostgreSQL 11的安裝和配置教程
  • Ubuntu PostgreSQL安裝和配置的介紹
  • Windows上PostgreSQL安裝配置教程
  • CentOS7 PostgreSQL安裝、配置、使用詳解
  • Debian中PostgreSQL數(shù)據(jù)庫安裝配置實例
  • postgresql安裝及配置超詳細教程

標簽:湖南 崇左 衡水 銅川 黃山 仙桃 蘭州 湘潭

巨人網(wǎng)絡(luò)通訊聲明:本文標題《PostgreSQL安裝、配置及簡單使用方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    屯留县| 天镇县| 嘉峪关市| 长岭县| 永丰县| 图们市| 邢台县| 黔南| 钟祥市| 深泽县| 安仁县| 娱乐| 南澳县| 垣曲县| 西畴县| 沂源县| 清镇市| 卓尼县| 襄汾县| 米泉市| 潞城市| 长岭县| 瑞昌市| 安丘市| 烟台市| 潍坊市| 龙江县| 内黄县| 瑞昌市| 伊金霍洛旗| 马山县| 兴山县| 五原县| 卢龙县| 阜阳市| 阿拉善左旗| 九台市| 营山县| 沙雅县| 桐庐县| 高青县|