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

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

二、PostgreSQL介绍

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


三、PostgreSQL安装

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

[root@postgresql ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)


安装准备

修改主机名
# 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

yum list | grep postgresql-server
yum install -y postgresql-server.x86_64

db0d43e3bca75b752c480a5f2c4ff4de.png

2、初始化postgresql-server数据库

service postgresql initdb

# service postgresql initdb
Hint: the preferred way to do this is now "postgresql-setup initdb"
Initializing database ... OK

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

systemctl start postgresql
systemctl enable postgresql

4、查看postgresql服务状态

systemctl status postgresql

5、查看服务进程信息

[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服务端口是否开启

# 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 ~]# 
# 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用户

[root@postgresql ~]# su postgres
[postgres@postgresql root]$

2、连接数据库

[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、创建数据库

# 创建一个 runoobdb 的数据库
postgres=# CREATE DATABASE xybdiy;
CREATE DATABASE
postgres=# 
# 使用 \c + 数据库名 来进入数据库
postgres=# \c xybdiy
You are now connected to database "xybdiy" as user "postgres".
xybdiy=# 

4、创建表格

# 创建了一个表,表名为 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的配置文件

# vim /var/lib/pgsql/data/postgresql.conf
# 修改监听IP
listen_addresses = '*'
# 打开日志采集器
logging_collector = on
# 设置日志目录
log_directory = 'pg_log'


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

# 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服务

# systemctl restart postgresql


五、测试远程连接

测试连接

a2a6db092041d14ec47c43910bb53d75.png

测试成功后,连接

b1b675f0c838ee810724da0b9c37e73b.png

连接成功

cdf1302dbf0fbb0c3e63b43e086b5a13.png

7453fbfdeddcc8b5f03cedaeb07f8de5.png

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

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍如何基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
5月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
1038 152
|
5月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
814 156
|
8月前
|
SQL 关系型数据库 MySQL
Go语言数据库编程:使用 `database/sql` 与 MySQL/PostgreSQL
Go语言通过`database/sql`标准库提供统一数据库操作接口,支持MySQL、PostgreSQL等多种数据库。本文介绍了驱动安装、连接数据库、基本增删改查操作、预处理语句、事务处理及错误管理等内容,涵盖实际开发中常用的技巧与注意事项,适合快速掌握Go语言数据库编程基础。
918 213
|
5月前
|
关系型数据库 MySQL 分布式数据库
阿里云PolarDB云原生数据库收费价格:MySQL和PostgreSQL详细介绍
阿里云PolarDB兼容MySQL、PostgreSQL及Oracle语法,支持集中式与分布式架构。标准版2核4G年费1116元起,企业版最高性能达4核16G,支持HTAP与多级高可用,广泛应用于金融、政务、互联网等领域,TCO成本降低50%。
|
5月前
|
关系型数据库 分布式数据库 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
阿里云数据库提供多种类型,包括关系型与NoSQL,主流如PolarDB、RDS MySQL/PostgreSQL、Redis等。价格低至21元/月起,支持按需付费与优惠套餐,适用于各类应用场景。
|
6月前
|
存储 关系型数据库 数据库
【赵渝强老师】PostgreSQL数据库的WAL日志与数据写入的过程
PostgreSQL中的WAL(预写日志)是保证数据完整性的关键技术。在数据修改前,系统会先将日志写入WAL,确保宕机时可通过日志恢复数据。它减少了磁盘I/O,提升了性能,并支持手动切换日志文件。WAL文件默认存储在pg_wal目录下,采用16进制命名规则。此外,PostgreSQL提供pg_waldump工具解析日志内容。
620 0
|
8月前
|
存储 关系型数据库 分布式数据库
【赵渝强老师】基于PostgreSQL的分布式数据库:Citus
Citus 是基于 PostgreSQL 的开源分布式数据库,采用 shared nothing 架构,具备良好的扩展性。它以插件形式集成,部署简单,适用于处理大规模数据和高并发场景。本文介绍了 Citus 的基础概念、安装配置步骤及其在单机环境下的集群搭建方法。
749 2
|
9月前
|
关系型数据库 MySQL Linux
CentOS系统安装phpStudy的详细步骤和注意事项
一、安装流程 执行官方安装脚本 通过以下命令直接安装官方集成环境(支持CentOS 7及以上版本): ``` yum install -y wget && wget -O install.sh https://www.hsbang.com/ install.sh && sh install.sh ``` 安装过程包含自动下载组件和配置环境,需等待2-5分钟。
436 4
|
10月前
|
关系型数据库 MySQL Linux
CentOS 7系统下详细安装MySQL 5.7的步骤:包括密码配置、字符集配置、远程连接配置
以上就是在CentOS 7系统下安装MySQL 5.7的详细步骤。希望这个指南能帮助你顺利完成安装。
2553 26
|
10月前
|
SQL 关系型数据库 数据库
【赵渝强老师】创建PostgreSQL的数据库
本文介绍了在PostgreSQL中通过SQL命令“create database”创建数据库的方法。首先查询系统目录pg_database以查看现有数据库集合,然后使用“create database”命令创建新数据库,并了解其在$PDATA/base目录下对应的文件夹生成。最后重新查询数据库集合确认创建结果,附带视频讲解便于理解操作步骤及注意事项。
284 1