前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【PostgreSQL】基于CentOS系统安装PostgreSQL数据库

【PostgreSQL】基于CentOS系统安装PostgreSQL数据库

作者头像
宝耶需努力
发布2022-12-13 15:24:13
2.1K0
发布2022-12-13 15:24:13
举报
文章被收录于专栏:Cloud-DIYCloud-DIY

一、参考链接

阿里巴巴开源镜像站-OPSX镜像站-阿里云开发者社区

postgresql镜像-postgresql下载地址-postgresql安装教程-阿里巴巴开源镜像站

二、PostgreSQL介绍

PostgreSQL是一种特性非常齐全的自由软件的对象-关系型数据库管理系统(ORDBMS),是以加州大学计算机系开发的POSTGRES,4.2版本为基础的对象关系型数据库管理系统。POSTGRES的许多领先概念只是在比较迟的时候才出现在商业网站数据库中。PostgreSQL支持大部分的SQL标准并且提供了很多其他现代特性,如复杂查询、外键、触发器、视图、事务完整性、多版本并发控制等。同样,PostgreSQL也可以用许多方法扩展,例如通过增加新的数据类型、函数、操作符、聚集函数、索引方法、过程语言等。另外,因为许可证的灵活,任何人都可以以任何目的免费使用、修改和分发PostgreSQL。(——PostgreSQL_百度百科)

三、PostgreSQL安装

本实验基于CentOS 7.9系统进行演示操作

代码语言:javascript
复制
[root@postgresql ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

安装准备

代码语言:javascript
复制
修改主机名
# hostnamectl set-hostname prostgresql

关闭防火墙
# systemctl stop firewalld
# systemctl disable firewalld

关闭SELinux安全模式
# setenforce 0
# getenforce 

配置网络信息并测试连通性
vim /etc/sysconfig/network-scripts/ifcfg-ens32
主要修改如下参数信息即可。
BOOTPROTO=static
ONBOOT=yes
IPADDR=192.168.200.25
PREFIX=24
GATEWAY=192.168.200.1
DNS1=192.168.200.1
按:wq保存退出。

重启网卡
# systemctl restart network
# ping bing.com

配置阿里云CentOS YUM源,加快镜像访问下载
参考链接:https://blog.csdn.net/qq_45392321/article/details/121450443
# yum clean all
# yum makecache
# yum repolist

升级系统🆙
# yum update

检查postgresql是否安装
# rpm -qa | grep postgre

检查PostgreSQL 安装位置
# rpm -qal | grep postgres

新增postgres用户组
# groupadd postgres

新增postgres用户并且设置这个postgres用户属于创建的postgres用户组
# useradd -g postgres postgres

修改postgres用户密码
[root@postgresql ~]# passwd postgres
Changing password for user postgres.
New password: 
BAD PASSWORD: The password is a palindrome
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@postgresql ~]# 

重启系统
reboot

1、查询并安装postgresql-server

代码语言:javascript
复制
yum list | grep postgresql-server

yum install -y postgresql-server.x86_64
image-20220224135741784
image-20220224135741784

2、初始化postgresql-server数据库

service postgresql initdb

代码语言:javascript
复制
# service postgresql initdb
Hint: the preferred way to do this is now "postgresql-setup initdb"
Initializing database ... OK

3、启动postgresql服务并设置开机自启动

代码语言:javascript
复制
systemctl start postgresql
systemctl enable postgresql

4、查看postgresql服务状态

代码语言:javascript
复制
systemctl status postgresql

5、查看服务进程信息

代码语言:javascript
复制
[root@postgresql ~]# ps -ef | grep postgres
postgres   1405      1  0 16:05 ?        00:00:00 /usr/bin/postgres -D /var/lib/pgsql/data -p 5432
postgres   1406   1405  0 16:05 ?        00:00:00 postgres: logger process   
postgres   1408   1405  0 16:05 ?        00:00:00 postgres: checkpointer process   
postgres   1409   1405  0 16:05 ?        00:00:00 postgres: writer process   
postgres   1410   1405  0 16:05 ?        00:00:00 postgres: wal writer process   
postgres   1411   1405  0 16:05 ?        00:00:00 postgres: autovacuum launcher process   
postgres   1412   1405  0 16:05 ?        00:00:00 postgres: stats collector process   
root       1440   1131  0 16:07 pts/0    00:00:00 grep --color=auto postgres
[root@postgresql ~]# 

6、查看postgresql服务端口是否开启

代码语言:javascript
复制
# ss -tunpl | grep postgres
tcp    LISTEN     0      128    127.0.0.1:5432                  *:*                   users:(("postgres",pid=1349,fd=4))
tcp    LISTEN     0      128       [::1]:5432               [::]:*                   users:(("postgres",pid=1349,fd=3))
[root@postgresql ~]# 
代码语言:javascript
复制
# netstat -tunpl | grep 5432
tcp    0   0 127.0.0.1:5432    0.0.0.0:*    LISTEN      1349/postgres 
tcp6   0   0 ::1:5432          :::*         LISTEN      1349/postgres

四、测试连接

1、切换postgres用户

代码语言:javascript
复制
[root@postgresql ~]# su postgres
[postgres@postgresql root]$ 

2、连接数据库

代码语言:javascript
复制
[root@postgresql ~]# su postgres
[postgres@postgresql root]$ psql -U postgres
could not change directory to "/root"
psql (9.2.24)
Type "help" for help.

postgres=# 

# 使用 \l 用于查看已经存在的数据库:
postgres=# \l
                             List of databases
   Name    |  Owner   | Encoding  | Collate | Ctype |   Access privileges   
-----------+----------+-----------+---------+-------+-----------------------
 postgres  | postgres | SQL_ASCII | C       | C     | 
 template0 | postgres | SQL_ASCII | C       | C     | =c/postgres          +
           |          |           |         |       | postgres=CTc/postgres
 template1 | postgres | SQL_ASCII | C       | C     | =c/postgres          +
           |          |           |         |       | postgres=CTc/postgres
(3 rows)

postgres=# 

# 进入命令行工具,可以使用 \help 来查看各个命令的语法
postgres-# \help

3、创建数据库

代码语言:javascript
复制
# 创建一个 runoobdb 的数据库
postgres=# CREATE DATABASE xybdiy;
CREATE DATABASE
postgres=# 

# 使用 \c + 数据库名 来进入数据库
postgres=# \c xybdiy
You are now connected to database "xybdiy" as user "postgres".
xybdiy=# 

4、创建表格

代码语言:javascript
复制
# 创建了一个表,表名为 COMPANY 表格,主键为 ID,NOT NULL 表示字段不允许包含 NULL 值
xybdiy=# CREATE TABLE COMPANY(
xybdiy(#    ID INT PRIMARY KEY     NOT NULL,
xybdiy(#    NAME           TEXT    NOT NULL,
xybdiy(#    AGE            INT     NOT NULL,
xybdiy(#    ADDRESS        CHAR(50),
xybdiy(#    SALARY         REAL
xybdiy(# );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "company_pkey" for table "company"
CREATE TABLE

# 使用 \d 命令来查看表格是否创建成功
xybdiy=# \d
          List of relations
 Schema |  Name   | Type  |  Owner   
--------+---------+-------+----------
 public | company | table | postgres
(1 row)

xybdiy=# CREATE TABLE DEPARTMENT(
xybdiy(#    ID INT PRIMARY KEY      NOT NULL,
xybdiy(#    DEPT           CHAR(50) NOT NULL,
xybdiy(#    EMP_ID         INT      NOT NULL
xybdiy(# );
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "department_pkey" for table "department"
CREATE TABLE
xybdiy=# \d
           List of relations
 Schema |    Name    | Type  |  Owner   
--------+------------+-------+----------
 public | company    | table | postgres
 public | department | table | postgres
(2 rows)

xybdiy=# 

五、修改配置文件

1、修改postgresql的配置文件

代码语言:javascript
复制
# vim /var/lib/pgsql/data/postgresql.conf
# 修改监听IP
listen_addresses = '*'

# 打开日志采集器
logging_collector = on

# 设置日志目录
log_directory = 'pg_log'

2、修改 pg_hba.conf 服务连接配置文件

代码语言:javascript
复制
# vim /var/lib/pgsql/data/pg_hba.conf
 77 # TYPE  DATABASE   USER      ADDRESS                 METHOD
 78 
 79 # "local" is for Unix domain socket connections only
 80 local   all        all                               trust
 81 # IPv4 local connections:
 82 host    all        all          127.0.0.1/32         trust
 83 host    all        all          0.0.0.0/0            trust
 84 # IPv6 local connections:
 85 host    all        all          ::1/128               md5

3、重启postgresql服务

代码语言:javascript
复制
# systemctl restart postgresql

五、测试远程连接

测试连接

image-20220225171841803
image-20220225171841803

测试成功后,连接

image-20220225171804258
image-20220225171804258

连接成功

image-20220225172044099
image-20220225172044099
image-20220225172032907
image-20220225172032907

至此,安装PostgreSQL数据库完成。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-02-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、参考链接
  • 二、PostgreSQL介绍
  • 三、PostgreSQL安装
    • 安装准备
      • 1、查询并安装postgresql-server
        • 2、初始化postgresql-server数据库
          • 3、启动postgresql服务并设置开机自启动
            • 4、查看postgresql服务状态
              • 5、查看服务进程信息
                • 6、查看postgresql服务端口是否开启
                • 四、测试连接
                  • 1、切换postgres用户
                    • 2、连接数据库
                      • 3、创建数据库
                        • 4、创建表格
                        • 五、修改配置文件
                          • 1、修改postgresql的配置文件
                            • 2、修改 pg_hba.conf 服务连接配置文件
                              • 3、重启postgresql服务
                              • 五、测试远程连接
                              相关产品与服务
                              数据库管理
                              数据库管理(Database Management Center,DMC)是一个高效,安全,可靠的数据库一站式管理平台。DMC 提供可视化的库管理、实例会话管理、SQL 窗口、SQL 安全审计、SQL 变更审批、实时监控、操作审计等数据库管理能力,集成诊断优化和数据可视化分析能力,从而简化和规范数据库管理操作、降低数据库运维门槛、提升运维效率。
                              领券
                              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档