一、PostgreSQL簡介
1、什么是PostgreSQL
PostgreSQL數(shù)據(jù)庫是目前功能最強(qiáng)大的開源數(shù)據(jù)庫,支持豐富的數(shù)據(jù)類型(如JSON何JSONB類型,數(shù)組類型)和自定義類型。而且它提供了豐富的接口,可以很容易地?cái)U(kuò)展它的功能,如可以在GiST框架下實(shí)現(xiàn)自己的索引類型等,它還支持使用C語言寫自定義函數(shù)、觸發(fā)器,也支持使用流行的語言寫自定義函數(shù),比如其中的PL/Perl提供了使用Perl語言寫自定義函數(shù)的功能,當(dāng)然還有PL/Python、PL/Tcl,等等。
2、PostgreSQL數(shù)據(jù)庫的優(yōu)勢
PostgreSQL數(shù)據(jù)庫是目前功能最強(qiáng)大的開源數(shù)據(jù)庫,它是最接近工業(yè)標(biāo)準(zhǔn)SQL92的查詢語言,并且正在實(shí)現(xiàn)新的功能已兼容最新的SQL標(biāo)準(zhǔn):SQL2003.
穩(wěn)定可靠:PostgreSQL是唯一能做到數(shù)據(jù)零丟失的開源數(shù)據(jù)庫。有報(bào)道稱國外的部分銀行也在使用PostgreSQL。
開源省錢:PostgreSQL數(shù)據(jù)庫是開源的、免費(fèi)的,而且是BSD協(xié)議,在使用和二次開發(fā)上基本沒有限制。
支持廣泛:PostgreSQL數(shù)據(jù)庫支持大量的主流開發(fā)語言,包括C、C++、Perl、Python、Java、Tcl,和PHP等。
PostgreSQL社區(qū)活躍:PostgreSQL基本上每三個(gè)月推出一個(gè)補(bǔ)丁版本,這意味著已知的BUG很快會(huì)被修復(fù),有應(yīng)用場景的需求也會(huì)及時(shí)得到響應(yīng)。
二、PostgreSQL安裝與配置
#安裝前準(zhǔn)備:
1、系統(tǒng)版本
[root@node1 ~]# cat /etc/redhat-releaseCentOS 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、#啟動(dòng)postgresql#直接啟動(dòng)會(huì)報(bào)錯(cuò):[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.#上面是提示數(shù)據(jù)庫還沒有初始化,所以我們先初始化一下postgresql-setup initdbInitializing database ... OK #提示初始化成功#重新啟動(dòng)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 #查看運(yùn)行狀態(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$ 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#到此為止,基本安裝已經(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.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#創(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$ 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#啟動(dòng)和停止數(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ù)庫,相當(dāng)于數(shù)據(jù)庫進(jìn)程立即停止,直接退出,下次啟動(dòng)數(shù)據(jù)庫需要進(jìn)行修復(fù)。
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ò)上的主機(jī)登錄這臺(tái)數(shù)據(jù)庫,需要把這個(gè)地址改成"*",或者0.0.0.0。port表示監(jiān)聽的端口,可以不更改,修改這兩個(gè)參數(shù)后,需要重啟才能生效。#與數(shù)據(jù)庫Log相關(guān)的參數(shù)logging_collector = on #日志的收集,on表示打開log_directory = 'pg_log' #定義日志的收集目錄日志的切換和是否選擇覆蓋則可以使用如下幾種方案方案一:每天生產(chǎn)一個(gè)新的日志文件log_filename = ‘postgresql-%Y-%m-%d_%H%M%S.log'log_truncate_on_rotation = offlog_rotation_age = 1dlog_rotation_size = 0方案二:每當(dāng)日志寫滿一定的大小(如10MB空間),則切換一個(gè)日志log_filename = ‘postgresql-%Y-%m-%d_%H%M%S.log'log_truncate_on_rotation = offlog_rotation_age = 0log_rotation_size = 10M方案三:只保留7天的日志,進(jìn)行循環(huán)覆蓋 log_filename = ‘postgresql-%a.log'log_truncate_on_rotation = offlog_rotation_age = 1dlog_rotation_size = 0
5、內(nèi)存參數(shù)的設(shè)置
shared_buffers:共享內(nèi)存的大小,主要用于共享數(shù)據(jù)塊。
#shared_buffers默認(rèn)值為32MB,如果有足夠的內(nèi)存,可以把這個(gè)參數(shù)改得大一些,這樣數(shù)據(jù)庫就可以緩存更多的數(shù)據(jù)庫,當(dāng)讀取數(shù)據(jù)時(shí),就可以從共享內(nèi)存中讀,而不需要再從文件上去讀取。
work_mem:單個(gè)SQL執(zhí)行時(shí),排序、hash join所使用的內(nèi)存,SQL運(yùn)行完后,內(nèi)存就釋放了,把這個(gè)值設(shè)大一些,會(huì)讓排序操作快一些。
三、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ù)庫查詢語句,基本及時(shí)SELECT查詢命令,用于數(shù)據(jù)查詢。
新聞熱點(diǎn)
疑難解答
圖片精選