一、前言
1、本文主要内容
- postgresql 12 安装(yum)
- postgresql 12 基础配置
- postgresql 12 远程访问配置
- postgresql 基础管理
2、本文环境信息与适用范围
- 环境信息
软件 | 版本 |
---|---|
centos | 7.6 release |
postgresql | 12.x |
- 适用范围
软件 | 版本 |
---|---|
centos | centos 7.x |
postgresql | 9.x-12.x |
二、postgresql安装
1、导入yum源
# centos 7 系统
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/el-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# centos 9 stream 系统
# sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/el-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# sudo dnf -qy module disable postgresql
复制
1、安装postgresql服务
# centos 7 系统
sudo yum install -y postgresql12 postgresql12-server
# centos 9 stream 系统
# sudo dnf install -y postgresql12-server
如果在安装过程出现了如下错误:
error: failed to download metadata for repo 'pgdg-common': repomd.xml gpg signature verification error: bad gpg signature
解决方法:只需要修改如下命令
sudo yum install -y --nogpgcheck postgresql12 postgresql12-server
复制
2、初始化数据库
sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
#initializing database ... ok
复制
3、启动postgresql服务
#启动postgresql服务
sudo systemctl start postgresql-12
#设置postgresql服务为开机启动
sudo systemctl enable postgresql-12
复制
二、修改postgres账号密码
postgresql安装成功之后,会默认创建一个名为postgres的linux用户,初始化数据库后,会有名为postgres的数据库,来存储数据库的基础信息,例如用户信息等等,相当于mysql中默认的名为mysql数据库。
postgres数据库中会初始化一名超级用户postgres
为了方便我们使用postgres账号进行管理,我们可以修改该账号的密码
1、进入postgresql命令行
通过su命令切换linux用户为postgres会自动进入命令行
su postgres
复制
2、启动sql shell
psql
复制
3、修改密码
alter user postgres with password 'newpassword';
复制
三、配置远程访问
1、开放端口
sudo firewall-cmd --add-port=5432/tcp --permanent
sudo firewall-cmd --reload
复制
2、修改ip绑定
#修改配置文件
vi /var/lib/pgsql/12/data/postgresql.conf
#将监听地址修改为*
#默认listen_addresses配置是注释掉的,所以可以直接在配置文件开头加入该行
listen_addresses='*'
复制
3、允许所有ip访问
#修改配置文件
vi /var/lib/pgsql/12/data/pg_hba.conf
#在文件尾部加入
host all all 0.0.0.0/0 md5
复制
4、重启postgresql服务
#重启postgresql服务
sudo systemctl restart postgresql-12
复制
配置完成后即可使用客户端进行连接
四、postgresql shell常用语法示例
启动sql shell:
su postgres
psql
复制
1、数据库相关语法示例
#创建数据库
create database mydb;
#查看所有数据库
\l
#切换当前数据库
\c mydb
#创建表
create table test(id int,body varchar(100));
#查看当前数据库下所有表
\d
复制
2、用户与访问授权语法示例
#新建用户
create user test with password 'test';
#赋予指定账户指定数据库所有权限
grant all privileges on database mydb to test;
#移除指定账户指定数据库所有权限
revoke all privileges on database mydb to test
3、小知识点
centos9 安装postgresql以及修改默认数据存储目录
复制
2> docker安装pgsql的指令:
docker run --name pgsql12 \
-p 1921:5432 \
-e postgresql_database=mkjshutter \
-e postgresql_postgres_password="postgresql#q1w2e3" \
-e postgresql_password="postgresql#q1w2e3" \
-v /home/data/pgsql/12/conf:/opt/bitnami/postgresql/conf/ \
-v /home/data/pgsql/12/data:/bitnami/postgresql \
--restart=always -d bitnami/postgresql:12.19.0
发表评论