PostgreSQL简介以及简单使用

1.简介

PostgreSQL 是一个免费的对象-关系数据库服务器(ORDBMS),在灵活的BSD许可证下发行。

PostgreSQL 开发者把它念作 post-gress-Q-L。

PostgreSQL 的 Slogan 是 "世界上最先进的开源关系型数据库"。

关于与mysql的区别参考:https://www.cnblogs.com/geekmao/p/8541817.html

2. 安装

1. 下载安装包:这里

 2. 安装

 直接下一步即可,需要注意:

(1) postgresql的默认账号是postgres

(2)postgresql的默认端口是5432

3.简单使用

 1. 登录

psql -U postgres

2.查看存在的库

l

3.创建数据库并且进入以及查看

postgres=# CREATE DATABASE test;
CREATE DATABASE
postgres=# c test;
您现在已经连接到数据库 "test",用户 "postgres".

4.创建表以及查看信息

test-# CREATE TABLE DEPARTMENT(
test(#    ID INT PRIMARY KEY      NOT NULL,
test(#    DEPT           CHAR(50) NOT NULL,
test(#    EMP_ID         INT      NOT NULL
test(# );
错误:  语法错误 在 "CREATE" 或附近的
第2行CREATE TABLE DEPARTMENT(
     ^
test=# CREATE TABLE DEPARTMENT(
test(#    ID INT PRIMARY KEY      NOT NULL,
test(#    DEPT           CHAR(50) NOT NULL,
test(#    EMP_ID         INT      NOT NULL
test(# );
CREATE TABLE
test=# d
                 关联列表
 架构模式 |    名称    |  类型  |  拥有者
----------+------------+--------+----------
 public   | department | 数据表 | postgres
(1 行记录)

test=# d department
               数据表 "public.department"
  栏位  |     类型      | Collation | Nullable | Default
--------+---------------+-----------+----------+---------
 id     | integer       |           | not null |
 dept   | character(50) |           | not null |
 emp_id | integer       |           | not null |
索引:
    "department_pkey" PRIMARY KEY, btree (id)

5. 插入一条数据以及搜索

test=# INSERT INTO department values(1, '测试', 1);
INSERT 0 1
test=# select * from department;
 id |                         dept                         | emp_id
----+------------------------------------------------------+--------
  1 | 测试                                                 |      1
(1 行记录)

6. 在插入一条进行分页查询(limit是取的行数,offset是偏移量,从0开始且包含offset指定的下标值)

test=# INSERT INTO department values(2, '测试2', 2);
INSERT 0 1
test=# select * from department limit 1 offset 1;
 id |                         dept                         | emp_id
----+------------------------------------------------------+--------
  2 | 测试2                                                |      2
(1 行记录)

7. explain简单分析(列出的信息少于mysql的explain分析)

test=# explain select * from department where id = 1;
                                     QUERY PLAN
------------------------------------------------------------------------------------
 Index Scan using department_pkey on department  (cost=0.15..8.17 rows=1 width=212)
   Index Cond: (id = 1)
(2 行记录)

  其他事务类似于mysql,锁也有共享锁和排它锁。也有JSON类型的字段,mysql新版本(应该是5.7开始)也是支持JSON类型的字段。也有全文索引的功能,mysql5.7之后也支持全文索引。

  只是公司用到了postgresql,所以简单的使用下postgresql。

补充:postgreSQL的schema模式

一个模式可以包含视图、索引、据类型、函数和操作符等。

相同的对象名称可以被用于不同的模式中而不会出现冲突,例如 schema1 和 myschema 都可以包含名为 mytable 的表。

使用模式的优势:

(1)允许多个用户使用一个数据库并且不会互相干扰。

(2)将数据库对象组织成逻辑组以便更容易管理。

(3)第三方应用的对象可以放在独立的模式中,这样它们就不会与其他对象的名称发生冲突。

模式类似于操作系统层的目录,但是模式不能嵌套。

postgres=# create schema myschema;  #创建模式
CREATE SCHEMA
postgres=# create table myschema.company(    #创建表且指定模式
postgres(#    ID   INT              NOT NULL,
postgres(#    NAME VARCHAR (20)     NOT NULL,
postgres(#    AGE  INT              NOT NULL,
postgres(#    ADDRESS  CHAR (25),
postgres(#    SALARY   DECIMAL (18, 2),
postgres(#    PRIMARY KEY (ID)
postgres(# );
CREATE TABLE
postgres=# select * from myschema.company;    #查询指定模式的表
 id | name | age | address | salary
----+------+-----+---------+--------
(0 行记录)


postgres=# DROP SCHEMA myschema CASCADE;    删除模式级联删除数据
注意:  递归删除 表 myschema.company
DROP SCHEMA

  默认的schema是public模式。

原文地址:https://www.cnblogs.com/qlqwjy/p/13642558.html