3

centos7安装postgres13.1数据库

 1 year ago
source link: https://blog.51cto.com/u_13753753/5755170
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

centos7安装postgres13.1数据库

精选 原创

雍州无名 2022-10-14 09:29:49 博主文章分类:postgresql ©著作权

文章标签 postgresql sql 创建用户 文章分类 其它 系统/运维 阅读数216

1.安装postgres的yum仓库

下载所需要的数据库版本:​ ​https://yum.postgresql.org/repopackages/​

centos7安装postgres13.1数据库_sql

安装yum仓库

yum install pgdg-redhat-repo-latest.noarch.rpm

2.安装postgresql

(1).先查看postgresql源
yum list|grep postgresql
centos7安装postgres13.1数据库_postgresql_02
(2).安装postgresql
yum install postgresql13-contrib postgresql13-server -y
centos7安装postgres13.1数据库_sql_03
(3).初始化postgresql
postgresql-13-setup initdb
centos7安装postgres13.1数据库_postgresql_04
数据目录:/var/lib/pgsql/13/data
安装目录:/usr/pgsql-13
(4).启动postgresql
systemctl start postgresql-13
systemctl enable postgresql-13.service

查看是否启动成功

systemctl status postgresql-13.service
centos7安装postgres13.1数据库_创建用户_05

3.登录postgresql并设置密码

postgresql在安装时默认添加用户postgres

su - postgres

然后输入以下命令进入postgres

centos7安装postgres13.1数据库_创建用户_06
ALTER USER postgres WITH PASSWORD '123456';
centos7安装postgres13.1数据库_创建用户_07
退出命令行:\q

centos7安装postgres13.1数据库_sql_08

备注其他:列出所有库\l  列出所有用户\du 列出库下所有表\d

postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)

postgres=#
postgres=#
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

postgres=#

4.默认情况下postgresql是不用密码不支持远程登录的。我们需要修改配置文件

vim /var/lib/pgsql/13/data/pg_hba.conf

host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 trust
host replication all 0.0.0.0/0 trust
centos7安装postgres13.1数据库_postgresql_09

再修改配置文件postgresql.conf

vim /var/lib/pgsql/13/data/postgresql.conf

listen_addresses = '*' # what IP address(es) to listen on;
centos7安装postgres13.1数据库_sql_10

重启postgresql

systemctl restart postgresql-13

5.创建数据库及创建用户

因为postgres属于superuser,我们需要创建部分低权限用户

(1).创建数据库
create database testdb;

centos7安装postgres13.1数据库_创建用户_11

(2).创建用户
create user testuser createdb login password '123456';

centos7安装postgres13.1数据库_创建用户_12

(3).将testdb所有权限赋给用户testuser
grant all on database testdb to testuser;

centos7安装postgres13.1数据库_创建用户_13

注:如果要删除此账户可以使用 drop role testuser; 

6.使用连接工具DBeaver连接PostgreSQL

用户名: postgres
数据库名:postgres
密码:123456
端口:5432

官方地址: ​https://www.postgresql.org/download/linux/redhat/​

  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK