postgresql12.2安装
官网: https://www.postgresql.org/
安装环境:postgresql12.2+centos7.0
1.下载文件postgresql-12.2.tar.bz2到soft文件夹下
wget https://ftp.postgresql.org/pub/source/v12.2/postgresql-12.2.tar.bz2
或者访问下载页面:postgres.cn/v2/download 选取下载安装版本
postgresql-12.2.tar.bz2
2.解压postgresql-12.2.tar.bz2 ,安装工具bzip2,并解压
cd /soft
yum -y install bzip2
tar -jxvf postgresql-12.2.tar.bz2
3.执行配置脚本前需安装gcc、readline-devel、zlib-devel,make
yum -y install gcc yum -y install readline-devel yum install -y zlib-devel yum install -y make
4.安装pgsql并编译
cd postgresql-12.2
./configure --prefix=/opt/pgsql
make && make install
出现:PostgreSQL installation complete.
5.增加新用户,系统提示要给定新用户密码
adduser postgres
passwd postgres
输入:123456
6.创建数据库目录
mkdir /opt/pgsql/data
7.更改用户
chown -R postgres:postgres /opt/pgsql/data
- 使用postgres帐号操作
su -postgres
- 初始化数据库
/opt/pgsql/bin/initdb -D /opt/pgsql/data
10.启动数据库
/opt/pgsql/bin/pg_ctl -D /opt/pgsql/data -l logfile start
/opt/pgsql/bin/pg_ctl -D /opt/pgsql/data -l logfile stop
11.假定数据库名为gerericdb)
/opt/pgsql/bin/createdb genericdb
12.进入数据库内部
/opt/pgsql/bin/psql genericdb
\q 退出数据库
exit 退出postgres用户
13.查看进程命令: 查看端口监听情况命令
ps -ef | grep postgres
安装工具:yum -y install net-tools
netstat -an | grep 5432 或者 lsof -i:5432
14.配置环境变量
vi /etc/profile
export PATH=/opt/pgsql/bin:$PATH
刷新环境变量:
source /etc/profile
或者
su -postgres
psql genericdb
-
配置远程访问
1.vi /opt/pgsql/data/postgresql.conf 打开注释
listen_addresses = '*'
2.vi /opt/pgsql/data/pg_hba.conf 打开注释添加
# TYPE DATABASE USER ADDRESS METHOD
host all all 0.0.0.0/0 md5
#备注任意IP地址 任意用户 无需密码验证可直接连接访问该主机的 所有数据库 md5或 trust
- 重启服务(停止后再启动)
--在su -postgres用户下执行
/opt/pgsql/bin/pg_ctl -D /opt/pgsql/data -l logfile stop
/opt/pgsql/bin/pg_ctl -D /opt/pgsql/data -l logfile start
18.开放端口 开放防火墙端口: 或停止防火墙:systemctl stop firewalld;查看防火墙状态 systemctl status firewalld
firewall-cmd --zone=public --add-port=5432/tcp --permanent
查看所有防火墙:
firewall-cmd --zone=public --list-ports
firewall-cmd --reload
19.修改密码
修改linux密码:
删除: passwd -d postgres
重置:passwd postgres
输入密码:123456
修改:postgresql密码
psql postgres
alter user postgres with password ‘123456’;
创建新用户:
create user postgresql with password ‘123456’;
grant all on database genericdb to postgresql;
报错时:ERROR: permission denied for table xxx
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO postgresql;
20.登录(dbeaver)成功