当前位置: 首页 > news >正文

PostgreSQL简单使用

一、PostgreSQL概念
特点
    开源与自由
    标准符合性
    数据类型丰富
    事务与并发
    扩展性
    安全性
优势
    高性能
    高可用性
    灵活性
    社区支持
    成本效益
PostgreSQL结构
 多层逻辑结构
    第一层:实例(xxx.xxx.xxx.xxx:port)
    第二层:数据库(每个实例下可有多个相互独立的数据库)
    第三层:Schema(每个数据库下包含多个Schema)
         
    实例就是指该数据库,对外表示的ip和端口。
    数据库就是数据仓库。
    schema是指模式,模式将多个用户之间相同名称的对象隔离开。
    tablespace是指表空间,存放数据库的一个逻辑空间,可以存放不同的数据库,对应在物理层面上是一个目录。

关系型数据库
第二代数据库是关系型数据库
实体->关系
实体是指库中的表。   关系是指,比如两张表之间存在外键,这外键就是它俩之间的关系。

第三代数据库是面向对象型数据库
对象->关系
对象是指库、表、函数等等。   关系是指,比如在库中建了个表,表中建了个视图、索引等。

Oracle、PostgreSQL都属于第二代关系型数据库和第三代关系型数据库的结合。


PostgreSQL,一个表空间可以让多个数据库使用,一个数据库可以使用多个表空间。(多对多)

二、PostgreSQL安装示例

源码编译安装
1、安装依赖包
[root@localhost ~]# dnf -y install gcc* make libicu libicu-devel readline-devel zlib zlib-devel2、编译安装
[root@localhost ~]# ls
anaconda-ks.cfg  postgresql-15.4.tar.gz
[root@localhost ~]# tar zxf postgresql-15.4.tar.gz 
[root@localhost ~]# cd postgresql-15.4
[root@localhost postgresql-15.4]# ls
aclocal.m4  configure     contrib    doc             HISTORY  Makefile  src
config      configure.ac  COPYRIGHT  GNUmakefile.in  INSTALL  README
[root@localhost postgresql-15.4]# ./configure --prefix=/usr/local/pgsql
[root@localhost postgresql-15.4]# echo $?
0
[root@localhost postgresql-15.4]# make && make install
[root@localhost postgresql-15.4]# echo $?
03、配置环境变量
[root@localhost postgresql-15.4]# adduser postgres 
[root@localhost postgresql-15.4]# mkdir /usr/local/pgsql/data
[root@localhost postgresql-15.4]# ll /usr/local/pgsql/
总计 20
drwxr-xr-x. 2 root root 4096  4月21日 09:19 bin
drwxr-xr-x. 2 root root 4096  4月21日 09:21 data
drwxr-xr-x. 6 root root 4096  4月21日 09:19 include
drwxr-xr-x. 4 root root 4096  4月21日 09:19 lib
drwxr-xr-x. 6 root root 4096  4月21日 09:19 share
[root@localhost postgresql-15.4]# ll /usr/local/
总计 44
drwxr-xr-x. 2 root root 4096 11月19日 22:13 bin
drwxr-xr-x. 2 root root 4096 11月19日 22:13 etc
drwxr-xr-x. 2 root root 4096 11月19日 22:13 games
drwxr-xr-x. 2 root root 4096 11月19日 22:13 include
drwxr-xr-x. 2 root root 4096 11月19日 22:13 lib
drwxr-xr-x. 3 root root 4096  3月 1日 17:26 lib64
drwxr-xr-x. 2 root root 4096 11月19日 22:13 libexec
drwxr-xr-x. 7 root root 4096  4月21日 09:21 pgsql
drwxr-xr-x. 2 root root 4096 11月19日 22:13 sbin
drwxr-xr-x. 5 root root 4096  3月 1日 17:26 share
drwxr-xr-x. 2 root root 4096 11月19日 22:13 src
[root@localhost postgresql-15.4]# chown postgres /usr/local/pgsql/data
[root@localhost postgresql-15.4]# ll /usr/local/pgsql/
总计 20
drwxr-xr-x. 2 root     root 4096  4月21日 09:19 bin
drwxr-xr-x. 2 postgres root 4096  4月21日 09:21 data
drwxr-xr-x. 6 root     root 4096  4月21日 09:19 include
drwxr-xr-x. 4 root     root 4096  4月21日 09:19 lib
drwxr-xr-x. 6 root     root 4096  4月21日 09:19 share
[root@localhost postgresql-15.4]# cat >> /etc/profile << 'EOF'
> LD_LIBRARY_PATH=/usr/local/pgsql/lib # 配置共享库
> export LD_LIBRARY_PATH
> PATH=/usr/local/pgsql/bin:$PATH  # 配置命令可搜索路径
> export PATH
> EOF
[root@localhost postgresql-15.4]# source /etc/profile4、登录数据库
[root@localhost postgresql-15.4]# su - postgres  # 以postgres用户的来完成后续的操作。[postgres@localhost ~]$ /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data 
# 用于初始化PostgreSQL数据库目录和数据文件。
# -D选项指定了数据库的数据目录。 
# initdb是postgresql的内部命令,用来创建并初始化一个新的postgresql数据库。The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.The database cluster will be initialized with locale "zh_CN.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"
The default text search configuration will be set to "simple".Data page checksums are disabled.fixing permissions on existing directory /usr/local/pgsql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... okinitdb: warning: enabling "trust" authentication for local connections
initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.Success. You can now start the database server using:/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start[postgres@localhost ~]$ echo $?
0
[postgres@localhost ~]$ /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start 
# -D /usr/local/pgsql/data指定了数据目录的位置,与上条命令相同。 
# start指示postgresql开始运行。
waiting for server to start.... done
server started
[postgres@localhost ~]$ echo $?
0
[postgres@localhost ~]$ /usr/local/pgsql/bin/psql
psql (15.4)
Type "help" for help.postgres=# [root@localhost ~]# dnf -y install tree[root@localhost ~]# tree -L 1 -d /usr/local/pgsql/data/
/usr/local/pgsql/data/
├── base
├── global
├── pg_commit_ts
├── pg_dynshmem
├── pg_logical
├── pg_multixact
├── pg_notify
├── pg_replslot
├── pg_serial
├── pg_snapshots
├── pg_stat
├── pg_stat_tmp
├── pg_subtrans
├── pg_tblspc
├── pg_twophase
├── pg_wal
└── pg_xact18 directories[root@localhost pgsql]# cd data
[root@localhost data]# ls
base          pg_ident.conf  pg_serial     pg_tblspc    postgresql.auto.conf
global        pg_logical     pg_snapshots  pg_twophase  postgresql.conf
pg_commit_ts  pg_multixact   pg_stat       PG_VERSION   postmaster.opts
pg_dynshmem   pg_notify      pg_stat_tmp   pg_wal       postmaster.pid
pg_hba.conf   pg_replslot    pg_subtrans   pg_xact
[root@localhost data]# cd base
[root@localhost base]# ls
1  4  5  # 每个数据库都会在$PGDATA/base下面生成一个子目录
[root@localhost base]# cd 1
[root@localhost 1]# ls
112        2337      2616_vm   2686      3079_vm   3596      4157
113        2579      2617      2687      3080      3597      4158
1247       2600      2617_fsm  2688      3081      3598      4159
1247_fsm   2600_fsm  2617_vm   2689      3085      3599      4160
-----略postgres=# select datname,oid from pg_database;datname  | oid 
-----------+-----postgres  |   5template1 |   1template0 |   4
(3 rows)
# 与上面的物理文件一一对应。yum安装
[root@localhost ~]# dnf -y install postgresql-server
[root@localhost ~]# postgresql-setup --initdb  # 初始化数据库。* Initializing database in '/var/lib/pgsql/data' * Initialized, logs are in /var/lib/pgsql/initdb_postgresql.log
[root@localhost ~]# su - postgres
[postgres@localhost ~]$ ls
backups  data  initdb_postgresql.log
[postgres@localhost ~]$ cat initdb_postgresql.log 
属于此数据库系统的文件宿主为用户 "postgres".
此用户也必须为服务器进程的宿主.
数据库簇将使用本地化语言 "zh_CN.UTF-8"进行初始化.
默认的数据库编码已经相应的设置为 "UTF8".
initdb: 无法为本地化语言环境"zh_CN.UTF-8"找到合适的文本搜索配置
缺省的文本搜索配置将会被设置到"simple"禁止为数据页生成校验和.修复已存在目录 /var/lib/pgsql/data 的权限 ... 成功
正在创建子目录 ... 成功
选择动态共享内存实现 ......posix
选择默认最大联接数 (max_connections) ... 100
选择默认共享缓冲区大小 (shared_buffers) ... 128MB
选择默认时区 ... Asia/Shanghai
创建配置文件 ... 成功
正在运行自举脚本 ...成功
正在执行自举后初始化 ...成功
同步数据到磁盘...成功成功。您现在可以用下面的命令开启数据库服务器:/usr/bin/pg_ctl -D /var/lib/pgsql/data -l 日志文件 start[postgres@localhost ~]$ /usr/bin/pg_ctl -D /var/lib/pgsql/data -l initdb_postgresql.log start
等待服务器进程启动 .... 完成
服务器进程已经启动
[postgres@localhost ~]$ ls
backups  data  initdb_postgresql.log
[postgres@localhost ~]$ cat initdb_postgresql.log 
属于此数据库系统的文件宿主为用户 "postgres".
此用户也必须为服务器进程的宿主.
数据库簇将使用本地化语言 "zh_CN.UTF-8"进行初始化.
默认的数据库编码已经相应的设置为 "UTF8".
initdb: 无法为本地化语言环境"zh_CN.UTF-8"找到合适的文本搜索配置
缺省的文本搜索配置将会被设置到"simple"禁止为数据页生成校验和.修复已存在目录 /var/lib/pgsql/data 的权限 ... 成功
正在创建子目录 ... 成功
选择动态共享内存实现 ......posix
选择默认最大联接数 (max_connections) ... 100
选择默认共享缓冲区大小 (shared_buffers) ... 128MB
选择默认时区 ... Asia/Shanghai
创建配置文件 ... 成功
正在运行自举脚本 ...成功
正在执行自举后初始化 ...成功
同步数据到磁盘...成功成功。您现在可以用下面的命令开启数据库服务器:/usr/bin/pg_ctl -D /var/lib/pgsql/data -l 日志文件 start2025-03-01 11:46:22.784 CST [2368] 日志:  日志输出重定向到日志收集进程
2025-03-01 11:46:22.784 CST [2368] 提示:  后续的日志输出将出现在目录 "log"中.
[postgres@localhost ~]$ psql
psql (15.12)
输入 "help" 来获取帮助信息.postgres=# exit
[postgres@localhost ~]$ psql
psql (15.12)
输入 "help" 来获取帮助信息.postgres=# 

http://www.xdnf.cn/news/556705.html

相关文章:

  • 深入浅出人工智能:机器学习、深度学习、强化学习原理详解与对比!
  • 【深度学习-Day 14】从零搭建你的第一个神经网络:多层感知器(MLP)详解
  • 第六天的尝试
  • 服务器部署1Panel
  • 證券行業證券交易系統開發方案
  • 基于SpringBoot+Vue的学籍管理系统的设计与实现
  • Kubernetes在线练习平台深度对比:KillerCoda与Play with Kubernetes
  • 【开源工具】文件夹结构映射工具 | PyQt5实现多模式目录复制详解
  • 【鸿蒙开发】Hi3861学习笔记- MQTT通信
  • 统一端点管理(UEM):定义、优势与重要性
  • 从零开始:Python 从0到1轻松入门
  • 易路 AI 招聘:RPA+AI 颠覆传统插件模式,全流程自动化实现效率跃迁
  • 物业收费智能化:如何实现账单零差错自动生成?
  • SpringBean模块(三)具有生命周期管理能力的类(1)AutowireCapableBeanFactory
  • DOS常用命令及dos运行java
  • 协程+Flow:现代异步编程范式,替代RxJava的完整实践指南
  • NVIDIA Earth-2 AI 天气模型 DLI 课程:解锁全球风云的未来之匙
  • 4大AI智能体平台,你更适合哪一个呐?
  • 第六部分:第三节 - 路由与请求处理:解析顾客的点单细节
  • ⭐️白嫖的阿里云认证⭐️ 第二弹【课时3:大模型辅助内容生产场景】for 「大模型Clouder认证:利用大模型提升内容生产能力」
  • 基于YOLO11深度学习的变压器漏油检测系统【Python源码+Pyqt5界面+数据集+安装使用教程+训练代码】【附下载链接】
  • 通过 API 获取 1688 平台店铺所有商品信息的完整流程
  • Vue+eElement ui el-input输入框 type=number 输入无效。赋值输入框也不显示(问题已解决)
  • FaceFusion 3.2.0 参数配置参考
  • Java实现定时任务的几种常见方式
  • 新闻媒体发稿:社会实践返家乡主题如何选择
  • 《扣子空间:开启AI智能体办公新时代》
  • DAY29 超大力王爱学Python
  • 理解阿里云的MQTT
  • 探秘「4+3原型驱动的交付模式」如何实现软件快速定制