CentOS7 yum PostgreSQL(9.4) 并启用 python3编写存储过程(Python3.3)

CentOS7 yum PostgreSQL(9.4) 并启用 python3编写存储过程(Python3.3)

yum 安装

1. 下载PostgreSQL

2. 安装PostgreSQL源

[root@controller02 ~]# yum install pgdg-centos94-9.4-3.noarch.rpm 

3. 安装PostgresSQl9.4 客户端与服务端

[root@controller02 ~]# yum install postgresql94 postgresql94-server

4. 初始化数据库

[root@controller02 lib]# /usr/pgsql-9.4/bin/postgresql94-setup initdb

数据库文件一般在/var/pgsql/9.4/目录下

5. 启动PostgreSQL服务

[root@controller02 ~]# systemctl start postgresql-9.4
[root@controller02 ~]# systemctl enable postgresql-9.4

6. 登陆postgreSQl 并修改密码

[root@controller02 ~]# su - postgres
Last login: Thu Mar 28 09:44:11 CST 2019 on pts/0
-bash-4.2$ psql
psql (9.4.21)
Type "help" for help.

postgres=# \password
Enter new password: 
Enter it again: 
  1. 设置远程方案
    1. 修改postgresql.conf 文件
    [root@controller02 ~]# find / -name postgresql.conf
    /var/lib/pgsql/9.4/data/postgresql.conf
    [root@controller02 ~]# vim /var/lib/pgsql/9.4/data/postgresql.conf
    # 找到listen_addresses 取消注释并修改成如下
    listen_addresses = '0.0.0.0'
    # listen_addresses = '*' 也可以
    
    CentOS7 yum PostgreSQL(9.4) 并启用 python3编写存储过程(Python3.3)_第1张图片
    2. 修改pg_hba.conf文件 最后一行添加
    [root@controller02 ~]# vim /var/lib/pgsql/9.4/data/pg_hba.conf 
    host    all             all             0.0.0.0/0              md5
    
    CentOS7 yum PostgreSQL(9.4) 并启用 python3编写存储过程(Python3.3)_第2张图片
    3. 打开防火墙5432端口并重启postgresql服务
    [root@controller02 data]# firewall-cmd --zone=public --add-port=5432/tcp --permanent
    success
    [root@controller02 data]# firewall-cmd --reload
    success
    [root@controller02 data]# firewall-cmd --zone=public --list-all
    public (active)
    target: default
    icmp-block-inversion: no
    interfaces: enp0s31f6
    sources: 
    services: ssh dhcpv6-client
    ports: 5432/tcp
    protocols: 
    masquerade: no
    forward-ports: 
    source-ports: 
    icmp-blocks: 
    rich rules: 
    
    [root@controller02 data]# systemctl restart postgresql-9.4
    

编译安装Python3.3

PostgreSQL版本与其对应支持的python3版本

PostgreSQL Version Python Version
9.X 3.3
10.X 3.4
11.X 3.6

1. 下载Python3.3

2. 安装可能需要的依赖包(dependence)

[root@controller02 ~]# yum install gcc openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel -y

3. 解压Python-3.3.6.tgz 并进入文件夹

[root@controller02 ~]# tar -zvxf Python-3.3.6.tgz
[root@controller02 ~]# cd Python-3.3.6

4. 安装Python3.3

[root@controller02 Python-3.3.6]# ./configure --prefix=/usr/local/Python33 --enable-shared
[root@controller02 Python-3.3.6]# make & make install

5. 添加软连接到 /usr/local/bin 下,以使用python3 命令

[root@controller02 ~]# ln -s /usr/local/Python33/bin/python3 /usr/local/bin/python3
[root@controller02 ~]# python3
python3: error while loading shared libraries: libpython3.3m.so.1.0: cannot open shared object file: No such file or directory

6. 添加python3.3动态库配置文件

[root@controller02 ~]# echo /usr/local/bin/lib > /etc/ld.so.conf.d/python3.3.conf
[root@controller02 ~]# ldconfig
[root@controller02 ~]# ldd /usr/local/bin/python3
        linux-vdso.so.1 =>  (0x00007ffcd8d9e000)
        libpython3.3m.so.1.0 => /usr/local/bin/lib/ libpython3.3m.so.1.0 (0x00007fd5cd547000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fd5cd32b000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007fd5cd127000)
        libutil.so.1 => /lib64/libutil.so.1 (0x00007fd5ccf24000)
        libm.so.6 => /lib64/libm.so.6 (0x00007fd5ccc22000)
        libc.so.6 => /lib64/libc.so.6 (0x00007fd5cc855000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fd5cda0d000)
[root@controller02 ~]# python
Python 2.7.5 (default, Apr 11 2018, 07:36:10) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@controller02 bin]# python3
Python 3.3.6 (default, Mar 28 2019, 09:38:14) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()

7. 下载 plpython3.so 并添加至 /usr/pgsql-9.4/lib

[root@controller02 ~]# chmod 755 plpython3.so

8. 创建plpython3u语言

postgres=# create language plpython3u;
CREATE LANGUAGE
postgres=# CREATE FUNCTION pymax (a integer, b integer)
postgres-#   RETURNS integer
postgres-# AS $$
postgres$#   if a > b:
postgres$#     return a
postgres$#   return b
postgres$# $$ LANGUAGE plpython3u;
CREATE FUNCTION
postgres=# select pymax(10,12);
 pymax 
-------
    12
(1 row)

CentOS7 yum PostgreSQL(9.4) 并启用 python3编写存储过程(Python3.3)_第3张图片






源码安装:未完成

1. 下载PostgreSQL

2. 安装gcc

# yum install gcc -y

3. 解压并编译安装

# tar -zvxf postgresql-10.7.tar.gz
# cd postgresql-10.7
# ./configure --with-python
# make && make install

4. 添加环境变量

# vim ~/.bash_profile
# 添加以下内容


# 添加结束 source配置文件
# source ~/.bash_profile

5. 创建postgres用户

# useradd postgres
# passwd postgres

6. 初始化数据库集群

$ initdb -D /usr/local/pgsql/data
#若出现错误查看data文件夹是否存在,若不存在则进行如下步骤创建
# mkdir -pv /usr/local/pgsql/data
# chowm -R postgres:postgres /usr/local/pgsql/data
#创建成功之后再执行initdb的步骤

7. 启动数据库服务器

[postgres@kolla ~]$ postgres -D /usr/local/pgsql/data >logfile 2>&1 &
#将服务器的stdout和stderr输出写入logfile文件中

8. 添加postgreSQL服务

[root@kolla ~]# vim /etc/systemd/system/postgresql.service
# 添加以下内容
[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)

[Service]
Type=notify
User=postgres
ExecStart=/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=0

[Install]
WantedBy=multi-user.target

出现的问题

testpy3=# create extension if not exists pypython3u;
ERROR:  could not open extension control file "/usr/local/pgsql/share/extension/pypython3u.control": No such file or directory
testpy3=# create extension if not exists plpython3u;
ERROR:  could not access file "$libdir/plpython3": No such file or directory

你可能感兴趣的:(Linux,CentOS7,Python3,PostgreSQL,plpython3u)