# PostgreSQL ## 下载 官网地址:[https://www.postgresql.org/](https://www.postgresql.org/) 下载地页面:[https://www.postgresql.org/download/linux/redhat/](https://www.postgresql.org/download/linux/redhat/) ## 安装 使用yum命令添加PostgreSQL软件源。 ``` [root@localhost ~] yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm ``` 使用yum命令安装PostgreSQL。 ``` [root@localhost ~] yum install postgresql12 [root@localhost ~] yum install postgresql12-server [root@localhost ~] yum install postgresql12-contrib ``` 初始化数据库。 ``` [root@localhost ~] /usr/pgsql-12/bin/postgresql-12-setup initdb ``` 配置开机启动与启动 ``` [root@localhost ~] systemctl enable postgresql-12 [root@localhost ~] systemctl start postgresql-12 ``` ## 配置远程访问 ### 修改密码 首先,切换到postgres用户。然后执行修改用户密码SQL语句。其中“postgres”为要修改密码的用户,“123456”为用户的密码。 ``` [root@localhost ~] su - postgres -bash-4.2$ psql postgres=# alter user postgres with password '123456'; postgres-# \q * 注意设置密码后面的分号不可以忽略 ``` ### 修改配置文件 打开并编辑文件“/var/lib/pgsql/12/data/postgresql.conf”。将“#listen_addresses = 'localhost'”改为“listen_addresses = '*'” ![PostgreSQL远程连接配置](./img/postgresql_001.png) 打开并编辑文件“/var/lib/pgsql/12/data/pg_hba.conf”。在文件的末尾添加“host all all 0.0.0.0/0 md5”。 ![PostgreSQL远程连接配置](./img/postgresql_002.png) ### 安装Python3支持包 如果不需要数据库Python3支持,则忽略该步骤。 ``` [root@localhost ~] yum install postgresql12-plpython3 ``` 要使用python扩展,需要打开数据库,执行如下SQL语句。 ``` sql CREATE extension plpython3u; ```