Ubuntu系统——操作PostgreSQL数据库

目录

一、安装PostgreSQL数据库

二、操作PostgreSQL数据库 

1.设置PostgreSQL数据库用户密码

2.进入postgres用户

3.查看所有数据库——\l(L的小写)

4.创建数据库

5.删除数据库

6.查看版本号

7.授权远程登录

三、PostgresSQL数据库的数据类型

1.数值型

2.字符串型

3.日期/时间型

4.布尔型

5.枚举型

6.几何类型

7.网络地址类型

8.二进制类型

9.数组类型


一、安装PostgreSQL数据库

[root@UbuntuGD ~]#sudo apt-get install postgresql -y

初次安装后,默认生成一个名为postgres的数据库和一个名为postgres的数据库用户;

同时还生成了一个名为postgres的Linux系统用户。

[root@UbuntuGD ~]#sudo systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor >
     Active: active (exited) since Sun 2024-03-24 16:06:47 CST; 28s ago
    Process: 3899 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 3899 (code=exited, status=0/SUCCESS)
        CPU: 1ms

Mar 24 16:06:47 UbuntuGD systemd[1]: Starting PostgreSQL RDBMS...
Mar 24 16:06:47 UbuntuGD systemd[1]: Finished PostgreSQL RDBMS.

二、操作PostgreSQL数据库 

1.设置PostgreSQL数据库用户密码

[root@UbuntuGD ~]#passwd postgres
New password: 
Retype new password: 
passwd: password updated successfully

2.进入postgres用户

[root@UbuntuGD ~]#su - postgres
postgres@UbuntuGD:~$ psql
psql (14.11 (Ubuntu 14.11-0ubuntu0.22.04.1))
Type "help" for help.

postgres=# \password postgres
#修改postgres数据库密码
Enter new password for user "postgres": 
Enter it again: 

3.查看所有数据库——\l(L的小写)

4.创建数据库

postgres=# create database class with owner=postgres encoding='UTF-8';
CREATE DATABASE

5.删除数据库

postgres=# drop database class;
DROP DATABASE

6.查看版本号

[root@UbuntuGD ~]#su - postgres
postgres@UbuntuGD:~$ psql --version
psql (PostgreSQL) 14.11 (Ubuntu 14.11-0ubuntu0.22.04.1)

7.授权远程登录

postgres@UbuntuGD:~$ psql -U postgres -d postgres -h 127.0.0.1 -p 5432
#授予psql数据库指定用户postgers用户 指定连接后的数据库是postgres 指定主机地址为127.0.0.1 指定端口为5432可以登入数据库
Password for user postgres: 
psql (14.11 (Ubuntu 14.11-0ubuntu0.22.04.1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

postgres=#
选项含义
-U指定用户,默认为当前用户
-d指定连接之后的数据库,默认是postgres
-h指定服务器地址,默认为127.0.0.1
-p指定端口号,默认为5432

三、PostgresSQL数据库的数据类型

1.数值型

  • smallint:2 字节的整数类型
  • integer:4 字节的整数类型
  • bigint:8 字节的整数类型
  • decimal:高精度小数类型
  • numeric:具有用户指定精度和范围的高精度小数类型(等同于 decimal)
  • real:单精度浮点数类型
  • double precision:双精度浮点数类型
  • serial:自动递增的 4 字节整数类型
  • bigserial:自动递增的 8 字节整数类型

2.字符串型

  • character(n):长度为 n 的字符类型
  • varchar(n):可变长度的字符类型,最大长度为 n
  • text:变长字符串类型,支持任意长度的字符串

3.日期/时间型

  • timestamp:日期和时间类型
  • date:日期类型
  • time:时间类型
  • interval:时间间隔类型

4.布尔型

boolean:布尔类型,只能取 true 或 false

5.枚举型

enum:枚举类型,用户定义的类型,只能从预定义的一组常量中选择一个值

6.几何类型

  • point:二维平面上的点
  • line:二维平面上的直线
  • lseg:二维平面上的线段
  • box:二维平面上的矩形
  • path:二维平面上的路径
  • polygon:二维平面上的多边形
  • circle:二维平面上的圆

7.网络地址类型

  • inet:IP 地址类型
  • cidr:IP 地址和掩码类型

8.二进制类型

bytea:二进制数据类型,用于存储字节序列

9.数组类型

  • int[]:整数数组类型
  • varchar(100)[]:字符串数组类型
  • timestamp[]:时间戳数组类型
  • 19
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值