共計 3020 個字符,預計需要花費 8 分鐘才能閱讀完成。
這篇文章主要介紹“PostgreSQL 的安裝和啟動方法”,在日常操作中,相信很多人在 PostgreSQL 的安裝和啟動方法問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”PostgreSQL 的安裝和啟動方法”的疑惑有所幫助!接下來,請跟著丸趣 TV 小編一起來學習吧!
官方文檔
二進制安裝和啟動:
https://www.postgresql.org/download/linux/redhat/
源碼安裝和啟動:
https://www.postgresql.org/docs/11/install-procedure.html
https://www.postgresql.org/docs/11/creating-cluster.html
https://www.postgresql.org/docs/11/server-start.html
https://www.postgresql.org/docs/current/kernel-resources.html
兩種方法
1、二進制安裝 (linux 是 rpm 包,windows 是 exe 安裝文件)
2、源碼安裝,推薦安裝方式
二進制安裝
示例:centos7_64 平臺,數據庫 postgresql11,使用 yum 安裝
不使用 yum 的話可以直接使用 rpm 包安裝,rpm 包下載地址 https://yum.postgresql.org/rpmchart.php
1、安裝 RPM 的 yum 源, 其實就是下載一個 postgresql 的 yum 源 pgdg-redhat-all.repo 文件到 /etc/yum.repos.d 目錄下,有了這個 yum 源后,就可以直接 yum install postgresql11 安裝 postgresql 數據庫了
yum install
https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
2、安裝客戶端,先執行 search 看有哪些 PostgreSQL client 可供安裝
yum search PostgreSQL client
yum install postgresql11
3、安裝服務端,先執行 search 看有哪些 PostgreSQL server 可供安裝,以下步驟安裝好后,會自動創建用戶 postgres,自動創建目錄 /usr/pgsql-11
yum search PostgreSQL server
yum install postgresql11-server
4、初始化數據庫并啟用開機自動啟動
/usr/pgsql-11/bin/postgresql-11-setup initdb
systemctl enable postgresql-11
systemctl start postgresql-11
5、查看 postgresql 的進程
[root@zabbixtest2 ~]# ps -ef|grep postgres
postgres 1527 1 0 01:13 ? 00:00:00 /usr/pgsql-11/bin/postmaster -D /var/lib/pgsql/11/data/
6、連接 postgresql 數據庫
su – postgres
psql -l
psql -d postgres
源碼安裝
1、建立 postgresql 用戶、內核資源的配置,主要涉及 /etc/security/limits.conf、/etc/sysctl.conf 文件,類似 oracle 也需要配置這兩個文件
2、解壓 tar 包,進入解壓目錄,使用 root 用戶執行如下,最后的 make install 命令把軟件安裝到 /postgresql/pgsql 目錄
./configure –prefix=/postgresql/pgsql
make
make install
3、把軟件安裝目錄 /postgresql/pgsql 授權宿主用戶為 postgresql
4、編輯 postgresql 用戶的.bash_profile 文件,PATH= /postgresql/pgsql/bin :$PATH、LD_LIBRARY_PATH= /postgresql/pgsql /lib
5、初始化數據庫,后面兩條命令任意一條都可以
su – postgresql
initdb -D /postgresql/pgsql/data
pg_ctl -D /postgresql/pgsq l/data initdb
6、啟動 postgresql 程序,下面任意一個都可以,官方文檔建議使用 pg_ctl
postgres -D /postgresql/pgsql/data logfile 2 1
pg_ctl start -D /postgresql/pgsql/data -l logfile
7、查看 postgresql 的進程
[root@zabbixtest1 ~]# ps -ef|grep postgres
postgre+ 803 1 0 07:10 pts/0 00:00:00 /postgresql/pgsql/bin/postgres -D /postgresql/pgsql/data
8、連接 postgresql 數據庫,指定連接 postgres 庫
psql -l
psql -d postgres
備注:psql 命令不加端口和不加數據庫名,表示默認進入端口為 5432 并且數據庫名和初始數據庫 initdb 時的用戶名一樣的數據庫,比如初始數據庫 initdb 時的用戶名為 A,則默認進入 A 庫。postgresql 的默認端口是 5432,默認數據庫是 postgres、template0、template1
如果此時端口不是 5432,則會報錯 psql: FATAL: role A does not exist
因為安裝用戶 A 對應的實例端口為其他,端口 5432 數據庫的 Owner 不是 A
如果此時沒有 A 庫,則會報錯 psql: FATAL: database A does not exist
因為安裝用戶 A,默認進入 A 庫,而 A 庫是不存在的
9、創建一個名為 test 的數據庫
createdb test
10、查看數據庫狀態
pg_ctl status -D /postgresql/pgsql/data
11、關閉數據庫
pg_ctl stop -D /postgresql/pgsql/data
官方文檔 Short Version 簡要步驟
./configure
make
su
make install
adduser postgres
mkdir /usr/local/pgsql/data
chown postgres /usr/local/pgsql/data
su – postgres
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data logfile 2 1
/usr/local/pgsql/bin/createdb test
/usr/local/pgsql/bin/psql test
到此,關于“PostgreSQL 的安裝和啟動方法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注丸趣 TV 網站,丸趣 TV 小編會繼續努力為大家帶來更多實用的文章!