PostgreSQL SQL操作简单入门

本文详细介绍了如何在PostgreSQL中创建和管理角色,包括赋予权限、创建数据库、修改用户密码,以及数据库的创建、查询、删除和表的操作,如表结构管理、JSON数据格式和FAQ解答。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.角色

查看角色
postgres-# \du
角色列表
角色名称 | 属性 | 成员属于
----------±-------------------------------------------±---------
postgres | 超级用户, 建立角色, 建立 DB, 复制, 绕过RLS | {}
创建角色
postgres=# CREATE ROLE test_role WITH LOGIN CREATEDB PASSWORD ‘test’;
CREATE ROLE
postgres=# \du
角色列表
角色名称 | 属性 | 成员属于
-----------±-------------------------------------------±---------
postgres | 超级用户, 建立角色, 建立 DB, 复制, 绕过RLS | {}
test_role | 建立 DB | {}
2.修改用户密码
postgres=# alter role postgres with password ‘postgres’;
ALTER ROLE

2.数据库

查询
postgres-# \l
数据库列表
名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限
-----------±---------±---------±---------------------------±---------------------------±----------------------
postgres | postgres | UTF8 | English_United States.1252 | English_United States.1252 |
template0 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 行记录)

创建
postgres=> CREATE DATABASE testdb OWNER test_role;
CREATE DATABASE
postgres=> \l
数据库列表
名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限
-----------±----------±---------±---------------------------±---------------------------±----------------------
postgres | postgres | UTF8 | English_United States.1252 | English_United States.1252 |
template0 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
testdb | test_role | UTF8 | English_United States.1252 | English_United States.1252 |
(4 行记录)

删除

postgres=> DROP DATABASE IF EXISTS testdb;
DROP DATABASE
postgres=> \l
数据库列表
名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限
-----------±---------±---------±---------------------------±---------------------------±----------------------
postgres | postgres | UTF8 | English_United States.1252 | English_United States.1252 |
template0 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 行记录)

3.表

下面的练习使用training数据库登录
Server [localhost]:
Database [postgres]: training
Port [5432]:
Username [postgres]: test_role
用户 test_role 的口令:

training=> \dt
Did not find any relations.
training=> CREATE TABLE STUDENT (
training(> SNO VARCHAR(20) PRIMARY KEY,
training(> SNAME VARCHAR(20),
training(> AGE INT,
training(> SEX CHAR(2) CHECK(SEX IN(‘男’,‘女’)),
training(> CREATED_AT TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW()
training(> );
CREATE TABLE
training=> \dt
关联列表
架构模式 | 名称 | 类型 | 拥有者
----------±--------±-------±----------
public | student | 数据表 | test_role
(1 行记录)
查看表结构
training=> \d student
数据表 “public.student”
栏位 | 类型 | Collation | Nullable | Default
------------±----------------------------±----------±---------±--------
sno | character varying(20) | | not null |
sname | character varying(20) | | |
age | integer | | |
sex | character(2) | | |
created_at | timestamp without time zone | | | now()
索引:
“student_pkey” PRIMARY KEY, btree (sno)
检查约束限制
“student_sex_check” CHECK (sex = ANY (ARRAY[‘男’::bpchar, ‘女’::bpchar]))
training=> INSERT INTO STUDENT VALUES(‘1’,‘李强’,23,‘男’);
INSERT 0 1
training=> SELECT * FROM student;
sno | sname | age | sex | created_at
-----±------±----±----±---------------------------
1 | 李强 | 23 | 男 | 2019-05-25 07:22:33.415539
(1 行记录)

training=> INSERT INTO STUDENT(SNO, SNAME, AGE,SEX) VALUES (‘2’,‘刘丽’,22,‘女’);
INSERT 0 1
training=> INSERT INTO STUDENT VALUES(‘5’,‘张友’,22,‘男’);
INSERT 0 1
training=> SELECT * FROM student;
sno | sname | age | sex | created_at
-----±------±----±----±---------------------------
1 | 李强 | 23 | 男 | 2019-05-25 07:22:33.415539
2 | 刘丽 | 22 | 女 | 2019-05-25 07:22:58.713986
5 | 张友 | 22 | 男 | 2019-05-25 07:23:07.742502
(3 行记录)
表中数据导出到文件
training=> \copy (SELECT * FROM student) TO ‘d:/student.csv’ WITH CSV HEADER;
COPY 3
d:/student.csv文件的内容如下:
sno,sname,age,sex,created_at
1,李强,23,男 ,2019-05-25 07:22:33.415539
2,刘丽,22,女 ,2019-05-25 07:22:58.713986
5,张友,22,男 ,2019-05-25 07:23:07.742502
表增加列
training=> ALTER TABLE student ADD COLUMN C1 INT;
ALTER TABLE
training=> \d student;
数据表 “public.student”
栏位 | 类型 | Collation | Nullable | Default
------------±----------------------------±----------±---------±--------
sno | character varying(20) | | not null |
sname | character varying(20) | | |
age | integer | | |
sex | character(2) | | |
created_at | timestamp without time zone | | | now()
c1 | integer | | |
索引:
“student_pkey” PRIMARY KEY, btree (sno)
检查约束限制
“student_sex_check” CHECK (sex = ANY (ARRAY[‘男’::bpchar, ‘女’::bpchar]))
表删除列
training=> ALTER TABLE STUDENT DROP COLUMN C1;
training=> \d student;
数据表 “public.student”
栏位 | 类型 | Collation | Nullable | Default
------------±----------------------------±----------±---------±--------
sno | character varying(20) | | not null |
sname | character varying(20) | | |
age | integer | | |
sex | character(2) | | |
created_at | timestamp without time zone | | | now()
索引:
“student_pkey” PRIMARY KEY, btree (sno)
检查约束限制
“student_sex_check” CHECK (sex = ANY (ARRAY[‘男’::bpchar, ‘女’::bpchar]))
JSON数据格式使用
CREATE TABLE test (
j JSON,
ja JSON[]
);
在这里插入图片描述

INSERT INTO test(ja) VALUES (
array[
‘{“name”:“alex”, “age”:20}’::json,
‘{“name”:“peter”, “age”:24}’::json
]
);
INSERT INTO test(j,ja) VALUES (
‘{“key”:“worker”, “company”:“pc”}’::json,
array[
‘{“name”:“alex”, “age”:20}’::json,
‘{“name”:“peter”, “age”:24}’::json
]
);
在这里插入图片描述

training=> delete from test where j is null;
在这里插入图片描述

training=> select j->>'key' as j_key from test;
 j_key
--------
 worker
(1 行记录)

从JSON提取数据

training=> SELECT '{"arr":[2,4,6,8]}'::json -> 'arr' -> 2 as new;
 new
-----
 6
(1 行记录)


training=> SELECT '{"arr":[2,4,6,8]}'::json #> ARRAY['arr','2'] as newcolumn;
 newcolumn
-----------
 6
(1 行记录)

4.FAQ

1.当命令输错了的时候,如何回到正常输入场景?
postgres-> \r
查询缓存区重置(已清空)。
postgres=>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值