1. 程式人生 > >postgresql學習之安裝篇

postgresql學習之安裝篇

執行 shu 修改 app exist home arc 加載 ova

---恢復內容開始---

安裝方法:

1、可以使用操作系統自帶的安裝源

2、可以使用官網下載的源碼進行安裝

3、可以使用編譯好的包入.run格式的安裝包安裝(本文使用的是這種安裝方法,下載地址http://www.postgres.cn/download)

安裝之前首先需要為postgresql數據庫新建一個管理員用戶:

  groupadd postgres

  mkdir /home/postgres

  useradd postgres -g postgres -s /bin/bash -b /home/postgres

開始安裝:

  安裝使用root用戶安裝,上傳了賦予文件執行權限chmod +x postgresql-9.4.5-1-linux-x64.ru。根據提示一路安裝至結束。安裝完了之後進入安裝目錄ll查看一下目錄信息,會發現.cache和data兩個目錄是postgres用戶組的。

配置:

進入data目錄下,配置文件說明:

  pg_hba.conf--此文件是配置鏈接權限的,共分5列,請求連接類型--要連接的數據庫--請求連接的用戶--請求來的地址--請求認證的方式。默認的第一行local all all md5,意思是本地登陸,我們本地登錄不驗證密碼,將md5換成trust

  pg_ident.conf--此文件是配置登陸數據庫所需要驗證的用戶名碼,格式username:password

  postgresql.conf--此文件是主要數據庫的配置信息,具體的每個參數含義不再列出,listen_addresses和port使用默認的。

啟動、關閉數據庫:

  為管理員用戶postgres配置環境變量。cd && vim .profile,我能當末尾添加如下信息:

export PG_HOME=/opt/PostgreSQL/9.4
export PGDATA=/opt/PostgreSQL/9.4/data
export PATH=$PATH:$PG_HOME/bin

當讓別忘記source .profile,下面就試試效果如何,直接輸入pg_ 然後使用TAB鍵會帶出很多pg_***的文件,這就說明環境變量已生效,那就開始啟動數據庫吧.

[email protected]:~$ pg_ctl start

pg_ctl: another server might be running; trying to start server anyway
server starting
[email protected]:~$ 2016-06-08 22:35:40 CST FATAL: lock file "postmaster.pid" already exists
2016-06-08 22:35:40 CST HINT: Is another postmaster (PID 2392) running in data directory "/opt/PostgreSQL/9.4/data"?

這個錯誤是說postmaster.pid這個文件已經存在了,說明這個數據庫已經被啟動了。那就先關掉吧

[email protected]:~$ pg_ctl stop
waiting for server to shut down.... done
server stopped

然後再啟動一下。

[email protected]:~$ pg_ctl start
server starting
[email protected]:~$ 2016-06-08 22:37:16 CST LOG: redirecting log output to logging collector process
2016-06-08 22:37:16 CST HINT: Future log output will appear in directory "pg_log".

[email protected]:~$ ps -ef|grep postgres --說明postgresql啟動成功
root 2660 1655 0 22:24 pts/2 00:00:00 su - postgres --這個用戶切換的進程,不用管
postgres 2661 2660 0 22:24 pts/2 00:00:00 -su --同上
postgres 2719 1 0 22:37 pts/2 00:00:00 /opt/PostgreSQL/9.4/bin/postgres --這個是postgresql的主進程
postgres 2720 2719 0 22:37 ? 00:00:00 postgres: logger process --這個是子進程,在PostgreSQL中稱為SysLogger(8.0),用於整個系統的日誌輸出;
postgres 2722 2719 0 22:37 ? 00:00:00 postgres: checkpointer process --這個是子進程,在PostgreSQL中稱為Checkpointer(9.2),用於處理checkpoints;
postgres 2723 2719 0 22:37 ? 00:00:00 postgres: writer process --這個是子進程,在PostgreSQL中稱為BgWriter,用於將臟頁刷出到磁盤;
postgres 2724 2719 0 22:37 ? 00:00:00 postgres: wal writer process --這個是子進程,在PostgreSQL中稱為WalWriter(8.3),處理預寫日誌輸出;
postgres 2725 2719 0 22:37 ? 00:00:00 postgres: autovacuum launcher process --這個是子進程,在PostgreSQL中稱為AutoVacuum(8.1),用於系統的自動清理;
postgres 2726 2719 0 22:37 ? 00:00:00 postgres: stats collector process --這個是子進程,在PostgreSQL中稱為PgStat,用於統計數據收集。
postgres 2728 2661 0 22:37 pts/2 00:00:00 ps -ef
postgres 2729 2661 0 22:37 pts/2 00:00:00 grep postgres

此外,如果配置了副本的話,應該還有一個進程 postgres: archiver process: 在PostgreSQL中稱為PgArch,用於預寫日誌歸檔;同步數據用的。

技術分享

詳細關於進程的可以參考http://wiki.postgresql.org/wiki/Pgsrcstructure

如果修改過配置需要執行pg_ctl reload重新加載數據庫

[email protected]:~$ psql -h 127.0.0.1 -p 5432
Password:
psql (9.3.10, server 9.4.5)
WARNING: psql major version 9.3, server major version 9.4.
Some psql features might not work.
Type "help" for help.

postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
postgres | postgres | UTF8 | zh_CN.utf8 | zh_CN.utf8 |
template0 | postgres | UTF8 | zh_CN.utf8 | zh_CN.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | zh_CN.utf8 | zh_CN.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)

postgresql學習之安裝篇