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

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

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

一、PostgreSQL簡介

1、什么是PostgreSQL

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

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

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

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

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

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

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


二、PostgreSQL安裝與配置

#安裝前準備:

1、系統(tǒng)版本

[root@node1 ~]# cat /etc/redhat-release
CentOS 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.service
Job for postgresql.service failed because the control process exited with error code. See "systemctl status postgresql.service" and "journalctl -xe" for details.
#上面是提示數據庫還沒有初始化,所以我們先初始化一下
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”用戶,登錄數據庫
[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
#到此為止,基本安裝已經完成。

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" #開發(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)建數據庫目錄并初始化數據庫
[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
#啟動和停止數據庫
# 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表示要監(jiān)聽的地址,要讓網絡上的主機登錄這臺數據庫,需要把這個地址改成"*",或者0.0.0.0。
port表示監(jiān)聽的端口,可以不更改,修改這兩個參數后,需要重啟才能生效。
#與數據庫Log相關的參數
logging_collector = on #日志的收集,on表示打開
log_directory = 'pg_log' #定義日志的收集目錄
日志的切換和是否選擇覆蓋則可以使用如下幾種方案
方案一:每天生產一個新的日志文件
log_filename = ‘postgresql-%Y-%m-%d_%H%M%S.log'
log_truncate_on_rotation = off
log_rotation_age = 1d
log_rotation_size = 0
方案二:每當日志寫滿一定的大?。ㄈ?0MB空間),則切換一個日志
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、內存參數的設置

shared_buffers:共享內存的大小,主要用于共享數據塊。

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

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


三、SQL語法入門

1、SQL語句語法簡介

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

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

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

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

相關文章

  • PostgreSQL自動更新時間戳實例代碼

    PostgreSQL自動更新時間戳實例代碼

    最近有這么一個工程,需要使用postgresql數據庫,在數據庫中的好幾個表中都需要時間戳這個字段,這篇文章主要給大家介紹了關于PostgreSQL自動更新時間戳的相關資料,需要的朋友可以參考下
    2021-11-11
  • CentOS 7下安裝PostgreSQL 9.6的教程分享

    CentOS 7下安裝PostgreSQL 9.6的教程分享

    PostgreSQL在我心目中的地位要遠遠高于MySQL,雖然流行對比MySQL低很對,但是功能性一致走在MySQL的前面。下面這篇文章主要介紹了CentOS 7下安裝PostgreSQL數據庫的方法,需要的朋友可以參考借鑒,一起來看看吧。
    2017-02-02
  • postgresql 查看當前用戶名的實現

    postgresql 查看當前用戶名的實現

    這篇文章主要介紹了postgresql 查看當前用戶名的實現,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • Postgresql源碼分析returns?setof函數oracle管道pipelined

    Postgresql源碼分析returns?setof函數oracle管道pipelined

    這篇文章主要為大家介紹了Postgresql源碼分析returns?setof函數oracle管道pipelined,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • PostgreSQL 實現列轉行問題

    PostgreSQL 實現列轉行問題

    這篇文章主要介紹了PostgreSQL 實現列轉行問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • PostgreSQL存儲過程用法實戰(zhàn)詳解

    PostgreSQL存儲過程用法實戰(zhàn)詳解

    這篇文章主要介紹了PostgreSQL存儲過程用法,結合具體實例詳細分析了PostgreSQL數據庫存儲過程的定義、使用方法及相關操作注意事項,并附帶一個完整實例供大家參考,需要的朋友可以參考下
    2018-08-08
  • PostgreSQL 遠程連接配置操作

    PostgreSQL 遠程連接配置操作

    這篇文章主要介紹了PostgreSQL 遠程連接配置操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • PostgreSQL中ON?CONFLICT的使用及一些擴展用法

    PostgreSQL中ON?CONFLICT的使用及一些擴展用法

    Postgres?ON?CONFLICT是PostgreSQL數據庫中的一個功能,用于處理插入或更新數據時的沖突情況,下面這篇文章主要給大家介紹了關于PostgreSQL中ON?CONFLICT的使用及一些擴展用法的相關資料,需要的朋友可以參考下
    2024-06-06
  • PostgreSQL教程(十六):系統(tǒng)視圖詳解

    PostgreSQL教程(十六):系統(tǒng)視圖詳解

    這篇文章主要介紹了PostgreSQL教程(十六):系統(tǒng)視圖詳解,本文講解了pg_tables、pg_indexes、pg_views、pg_user、pg_roles、pg_rules、pg_settings等視圖的作用和字段含義等內容,需要的朋友可以參考下
    2015-05-05
  • 關于向PostgreSQL數據庫插入Date類型數據報錯問題解決方案

    關于向PostgreSQL數據庫插入Date類型數據報錯問題解決方案

    本文給大家介紹在將數據庫從Oracle改為PostgreSQL時遇到的日期類型插入錯誤,通過使用PostgreSQL的特定語法和更改動態(tài)SQL語句解決了問題,本文給大家介紹的非常詳細,需要的朋友參考下吧
    2024-12-12

最新評論