網(wǎng)站開發(fā)者id百度號碼認(rèn)證平臺官網(wǎng)
PostgreSQL10數(shù)據(jù)庫源碼安裝及plpython2u、uuid-ossp插件安裝
- 1、環(huán)境
- 2、安裝包下載
- 3、安裝
- 3.1 、解壓
- 3.2、配置
- 3.3、編譯安裝
- 3.4 、啟動與關(guān)閉
- 4、安裝 uuid-ossp 、plpython2u插件
- 5、參考
1、環(huán)境
centos 7 、 postgresql 10.19
2、安裝包下載
postgres 源碼安裝包
3、安裝
3.1 、解壓
cd /tmp
tar -xzvf postgresql-10.19.tar.gz
3.2、配置
注:配置的時(shí)候一定要帶上這兩個(gè)選項(xiàng),因?yàn)楹竺嫖乙惭b uuid-ossp 、plpython2u插件
–with-python
–with-uuid=ossp \
./configure \
--prefix=/pg \
--exec-prefix=/pg \
--bindir=/pg/bin \
--sysconfdir=/pg/etc \
--libdir=/pg/lib \
--includedir=/pg/include --mandir=/pg/man --docdir=/pg/doc \
--htmldir=/pg/html \
--enable-nls='zh_CN en_US' \
--with-pgport=5432 \
--with-perl \
--with-python \
--with-tcl \
--with-icu \
--with-openssl \
--with-pam \
--with-ldap \
--with-systemd \
--with-readline \
--with-libxml \
--with-libxslt \
--with-segsize=1 \
--with-blocksize=8 \
--with-wal-segsize=16 \
--with-wal-blocksize=8 \
--with-uuid=ossp \
--without-zlib
配置的過程中,會提示缺少包,需要根據(jù)提示,手動安裝這些包
yum install -y gcc
yum install -y libicu-devel.x86_64
yum install -y perl-ExtUtils-Embed.noarch
yum install -y readline-devel.x86_64
yum -y install openssl-devel
yum -y install pam-devel.x86_64
yum install -y libxml2-devel.x86_64
yum install -y libxslt.x86_64
yum install -y libxslt-devel.x86_64
yum install -y openldap-devel.x86_64
yum install -y systemd-devel.x86_64
yum install -y tcl-devel.x86_64
yum install -y python-devel.x86_64
3.3、編譯安裝
1、編譯
首先進(jìn)行編譯,使用make命令進(jìn)行編譯,如果希望編譯所有的東西,包括文檔(man、html)和附加模塊(contrib),使用
make world
如果最后1行出現(xiàn)下面的描述,說明編譯成功
PostgreSQL, contrib, and documentation successfully made. Ready to install.
2、安裝
make install
當(dāng)最后1行出現(xiàn)"PostgreSQL installation complete."時(shí),說明安裝成功了。
NOTE:這條命令會把文件安裝到2.2指定的路徑,需要確保有足夠的權(quán)限向該區(qū)域?qū)懭搿MǔP枰褂胷oot權(quán)限操作,或者也可以事先創(chuàng)建目錄并分配權(quán)限給相應(yīng)的用戶
3、創(chuàng)建pg用戶
adduser postgres
passwd postgresmkdir -p /pg/data
chown -R postgres:postgres /pg/
4、初始化數(shù)據(jù)庫
su - postgres# 初始化
/pg/bin/initdb -D /pg/data
# 或者
/pg/bin/pg_ctl -D /pg/data initdb
5、設(shè)置環(huán)境變量
在postgres用戶下配置pg數(shù)據(jù)庫的環(huán)境變量
[postgres@pg01 ~]$ cat .bash_profile
# .bash_profile# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi# User specific environment and startup programsPATH=$PATH:$HOME/.local/bin:$HOME/bin:/pg/binexport PATHexport PGHOME=/pg
export PGDATA=/pg/data
export LD_LIBRARY_PATH=/pg/lib
export MANPATH=/pg/man
export PATH=/pg/bin:$PATH
[postgres@pg01 ~]$
6、使用source命令生效
[postgres@pg01 ~]$ source .bash_profile
3.4 、啟動與關(guān)閉
1、在postgresql用戶下使用pg_ctl
# 1.啟動數(shù)據(jù)庫
/pg/bin/postgres -D /pg/data >logfile 2>&1 &
# 或者可以使用pg_ctl命令
/pg/bin/pg_ctl -D /pg/data -l /tmp/logfile start# 2.關(guān)閉數(shù)據(jù)庫
/pg/bin/pg_ctl -D /pg/data -l /tmp/logfile stop# 3.查看數(shù)據(jù)庫狀態(tài)
/pg/bin/pg_ctl -D /pg/data status
2、使用root配置systemd管理數(shù)據(jù)庫
# 1.使用root用戶配置
su - rootvim /etc/systemd/system/postgresql.service[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)[Service]
Type=notify
User=postgres
ExecStart=/pg/bin/postgres -D /pg/data
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=0[Install]
WantedBy=multi-user.target# 2.重新加載systemd配置
[root@pg01 pg]# systemctl daemon-reload# 3.啟動、關(guān)閉、狀態(tài),使用root用戶
systemctl start postgresql
systemctl stop postgresql
systemctl status postgresql# 4.配置開機(jī)自啟動
systemctl enable postgresql
4、安裝 uuid-ossp 、plpython2u插件
我們有時(shí)候需要像Oracle或MySQL那樣可以通過函數(shù)sys_guid()或uuid()來獲得uuid,如:bdf7eb80-776f-11eb-b4e3-0800277427f4。默認(rèn)postgresql并沒有該功能,但是可以通過擴(kuò)展uuid-ossp來實(shí)現(xiàn)。
1、檢查是否已安裝擴(kuò)展uuid-ossp
postgres=# select * from pg_extension;oid | extname | extowner | extnamespace | extrelocatable | extversion | extconfig | extcondition
-------+---------+----------+--------------+----------------+------------+-----------+--------------13579 | plpgsql | 10 | 11 | f | 1.0 | |
(1 row)
可以看到postgresql目前沒有安裝uuid-ossp擴(kuò)展。
2、檢查是否有可用來安裝的擴(kuò)展uuid-ossp
查看當(dāng)前可用的擴(kuò)展:
postgres=# select * from pg_available_extensions;name | default_version | installed_version | comment
------------+-----------------+-------------------+-------------------------------------------plpgsql | 1.0 | 1.0 | PL/pgSQL procedural languageplperl | 1.0 | | PL/Perl procedural languageplperlu | 1.0 | | PL/PerlU untrusted procedural languageplpython2u | 1.0 | | PL/Python2U untrusted procedural languageplpythonu | 1.0 | | PL/PythonU untrusted procedural language
(5 rows)
可以看到postgres目前并沒有可用的uuid-ossp擴(kuò)展。
此時(shí),直接創(chuàng)建uuid-ossp會報(bào)錯(cuò),如:
postgres=# create extension "uuid-ossp";
ERROR: could not open extension control file "/opt/pgsql12.2/share/extension/uuid-ossp.control": No such file or directory
3、PG源碼文件下編譯安裝擴(kuò)展
3.1 安裝uuid依賴包
[root@Location-01 ~]# yum -y install e2fsprogs-devel uuid uuid-devel libuuid-devel
3.2 編譯安裝uuid-ossp
進(jìn)入擴(kuò)展目錄:(注意只有源碼安裝且要帶上 --with-uuid=ossp 選項(xiàng)才有這個(gè)目錄)
[root@VM_0_11_centos uuid-ossp]# pwd
/tmp/postgresql-10.19/contrib/uuid-ossp
[root@VM_0_11_centos uuid-ossp]# ls
expected sql uuid-ossp--1.1.sql uuid-ossp.control uuid-ossp.so
Makefile uuid-ossp--1.0--1.1.sql uuid-ossp.c uuid-ossp.o uuid-ossp--unpackaged--1.0.sql
3.3 執(zhí)行編譯安裝
[root@Location-01 uuid-ossp]# make && make install
3.4 創(chuàng)建擴(kuò)展
3.4.1 查看可用擴(kuò)展
postgres=# select * from pg_available_extensions;name | default_version | installed_version | comment
------------+-----------------+-------------------+-------------------------------------------------plpgsql | 1.0 | 1.0 | PL/pgSQL procedural languageplperl | 1.0 | | PL/Perl procedural languageplperlu | 1.0 | | PL/PerlU untrusted procedural languageplpython2u | 1.0 | | PL/Python2U untrusted procedural languageplpythonu | 1.0 | | PL/PythonU untrusted procedural languageuuid-ossp | 1.1 | | generate universally unique identifiers (UUIDs)
(6 rows)
可以看到已經(jīng)有擴(kuò)展uuid-ossp了。下面可以創(chuàng)建了。
postgres=# create extension "uuid-ossp";
CREATE EXTENSION
創(chuàng)建成功。
3.5 、使用擴(kuò)展
安裝擴(kuò)展成功以后,就可以使用函數(shù)uuid_generate_v4()來生產(chǎn)uuid了。
postgres=# select uuid_generate_v4();uuid_generate_v4
--------------------------------------cba03532-aa87-40be-b4e2-cf2e13b71c6e
(1 row)
4、安裝plpython2u插件
注意:在安裝postgres庫的時(shí)候一定要帶上 --with-python這個(gè)選項(xiàng),
postgres=# CREATE EXTENSION "plpython2u";
CREATE EXTENSION
5、參考
1、PostgreSQL10數(shù)據(jù)庫源碼安裝
2、postgresql12下安裝uuid-ossp