欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PostgreSQL安裝、配置及簡(jiǎn)單使用方法

 更新時(shí)間:2016年11月27日 22:27:52   投稿:mdxy-dxy  
這篇文章主要介紹了PostgreSQL安裝及簡(jiǎn)單使用方法,需要的朋友可以參考下

一、PostgreSQL簡(jiǎn)介

1、什么是PostgreSQL

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

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

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

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

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

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

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


二、PostgreSQL安裝與配置

#安裝前準(zhǔn)備:

1、系統(tǒng)版本

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

2、yum安裝(在官網(wǎng)上找到對(duì)應(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.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ù)庫(kù)還沒有初始化,所以我們先初始化一下
postgresql-setup initdb
Initializing database ... OK #提示初始化成功
#重新啟動(dòng)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 
#查看運(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ù)庫(kù)
[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ù)庫(kù)目錄并初始化數(shù)據(jù)庫(kù)
[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
#啟動(dòng)和停止數(shù)據(jù)庫(kù)
# pg_ctl start -D $PGDATA #PGDATA是pgsql的數(shù)據(jù)目錄
# pg_ctl stop -D $PGDATA [-m SHUTDOWN-MODE]
其中-m是制定數(shù)據(jù)庫(kù)的停止方法,有以下三種
smart:等所有的連接中止后,關(guān)閉數(shù)據(jù)庫(kù)。如果客戶端不中止,則無(wú)法關(guān)閉數(shù)據(jù)庫(kù)。
fast:快速關(guān)閉數(shù)據(jù)庫(kù),斷開客戶端的連接,讓已有的事務(wù)回滾,然后正常關(guān)閉數(shù)據(jù)庫(kù)。
immediate:立即關(guān)閉數(shù)據(jù)庫(kù),相當(dāng)于數(shù)據(jù)庫(kù)進(jìn)程立即停止,直接退出,下次啟動(dòng)數(shù)據(jù)庫(kù)需要進(jìn)行修復(fù)。

4、PostgreSQL的簡(jiǎn)單配置

在數(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ù)庫(kù),需要把這個(gè)地址改成"*",或者0.0.0.0。
port表示監(jiān)聽的端口,可以不更改,修改這兩個(gè)參數(shù)后,需要重啟才能生效。
#與數(shù)據(jù)庫(kù)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 = off
log_rotation_age = 1d
log_rotation_size = 0
方案二:每當(dāng)日志寫滿一定的大小(如10MB空間),則切換一個(gè)日志
log_filename = ‘postgresql-%Y-%m-%d_%H%M%S.log'
log_truncate_on_rotation = off
log_rotation_age = 0
log_rotation_size = 10M
方案三:只保留7天的日志,進(jìn)行循環(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默認(rèn)值為32MB,如果有足夠的內(nèi)存,可以把這個(gè)參數(shù)改得大一些,這樣數(shù)據(jù)庫(kù)就可以緩存更多的數(shù)據(jù)庫(kù),當(dāng)讀取數(shù)據(jù)時(shí),就可以從共享內(nèi)存中讀,而不需要再?gòu)奈募先プx取。

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


三、SQL語(yǔ)法入門

1、SQL語(yǔ)句語(yǔ)法簡(jiǎn)介

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

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

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

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

相關(guān)文章

  • PostgreSQL自定義函數(shù)并且調(diào)用方式

    PostgreSQL自定義函數(shù)并且調(diào)用方式

    這篇文章主要介紹了PostgreSQL如何自定義函數(shù)并且調(diào)用,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-06-06
  • postgresql之greenplum字符串去重拼接方式

    postgresql之greenplum字符串去重拼接方式

    這篇文章主要介紹了postgresql之greenplum字符串去重拼接方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • PostgreSQL 創(chuàng)建表分區(qū)

    PostgreSQL 創(chuàng)建表分區(qū)

    在pg里表分區(qū)是通過(guò)表繼承來(lái)實(shí)現(xiàn)的,一般都是建立一個(gè)主表,里面是空,然后每個(gè)分區(qū)都去繼承它。
    2009-09-09
  • 解決postgreSql遠(yuǎn)程連接數(shù)據(jù)庫(kù)超時(shí)的問題

    解決postgreSql遠(yuǎn)程連接數(shù)據(jù)庫(kù)超時(shí)的問題

    這篇文章主要介紹了解決postgreSql遠(yuǎn)程連接數(shù)據(jù)庫(kù)超時(shí)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • postgresql踩坑系列之關(guān)于to_date()問題

    postgresql踩坑系列之關(guān)于to_date()問題

    這篇文章主要介紹了postgresql踩坑系列之關(guān)于to_date()問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • postgreSQL 使用timestamp轉(zhuǎn)成date格式

    postgreSQL 使用timestamp轉(zhuǎn)成date格式

    這篇文章主要介紹了postgreSQL 使用timestamp轉(zhuǎn)成date格式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • PostgreSQL 對(duì)IN,EXISTS,ANY/ALL,JOIN的sql優(yōu)化方案

    PostgreSQL 對(duì)IN,EXISTS,ANY/ALL,JOIN的sql優(yōu)化方案

    這篇文章主要介紹了PostgreSQL 對(duì)IN,EXISTS,ANY/ALL,JOIN的sql優(yōu)化方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • postgres之jsonb屬性的使用操作

    postgres之jsonb屬性的使用操作

    這篇文章主要介紹了postgres之jsonb屬性的使用操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • PostgreSQL備份和還原命令整理大全

    PostgreSQL備份和還原命令整理大全

    這篇文章主要給大家介紹了關(guān)于PostgreSQL備份和還原命令整理的相關(guān)資料,PostgreSQL是一款非常強(qiáng)大的開源數(shù)據(jù)庫(kù)系統(tǒng),提供種類繁多的備份選項(xiàng),包括完全備份、增量備份等,需要的朋友可以參考下
    2023-07-07
  • Postgresql 實(shí)現(xiàn)查詢一個(gè)表/所有表的所有列名

    Postgresql 實(shí)現(xiàn)查詢一個(gè)表/所有表的所有列名

    這篇文章主要介紹了Postgresql 實(shí)現(xiàn)查詢一個(gè)表/所有表的所有列名,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12

最新評(píng)論