pg快速入门--初步使用

弓背霞明剑照霜,秋风走马出咸阳。这篇文章主要讲述pg快速入门--初步使用相关的知识,希望能为你提供帮助。
登录数据库

psql -U postgres -h localhost -p 5432



表空间
创建表空间
postgres=# create tablespace tbs_data1 owner postgres location /home/postgres;
CREATE TABLESPACE

查看表空间
postgres=# \\db
List of tablespaces
Name|Owner|Location
------------+----------+----------------
pg_default | postgres |
pg_global| postgres |
tbs_data1| postgres | /home/postgres
(3 rows)

删除表空间
postgres=# drop tablespace tbs_data1;
DROP TABLESPACE

select * from pg_tablespace;

如果我们不建立自己的表空间,建立表的时候,也不指定表空间。那么,PostgreSQL 不会建立你的表空间,所建立的表,都放入缺省表空间里。?
?
数据库
列出数据库以及数据库编码
postgres=# \\l
List of databases
Name|Owner| Encoding |Collate|Ctype|Access privileges
-----------+----------+----------+------------+------------+-----------------------
postgres| postgres | UTF8| en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8| en_US.utf8 | en_US.utf8 | =c/postgres+
||||| postgres=CTc/postgres
template1 | postgres | UTF8| en_US.utf8 | en_US.utf8 | =c/postgres+
||||| postgres=CTc/postgres
(3 rows)


select * from pg_database;

创建数据库
postgres=#create database mydb;
CREATE DATABASE
postgres=# \\l
List of databases
Name|Owner| Encoding |Collate|Ctype|Access privileges
-----------+----------+----------+------------+------------+-----------------------
mydb| postgres | UTF8| en_US.utf8 | en_US.utf8 |
postgres| postgres | UTF8| en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8| en_US.utf8 | en_US.utf8 | =c/postgres+
||||| postgres=CTc/postgres
template1 | postgres | UTF8| en_US.utf8 | en_US.utf8 | =c/postgres+
||||| postgres=CTc/postgres
(4 rows)


切换数据库
postgres=# \\c mydb;
You are now connected to database "mydb" as user "postgres".

查看当前的连接信息
\\conninfo
You are connected to database "mydb" as user "postgres" on host "localhost" (address "::1") at port "5432".

?
create table user_tbl(name varchar(20),signup_data DATE);

序列
创建序列
postgres=# CREATE SEQUENCE seqTest INCREMENT BY 1 START WITH 1 NO MAXvalue NO CYCLE CACHE 10;
CREATE SEQUENCE

查看序列
postgres=# select * from SEQTEST;
1 |0 | f

获取下一个序列号
postgres=# select nextval(SEQTEST);
1

postgres=# select nextval(SEQTEST);
2

postgres=# select nextval(SEQTEST);
3

postgres=# select nextval(SEQTEST);
4


序列相关的表或者视图
select * from pg_sequence;
select * from pg_sequences;
select * from information_schema.sequences;

?
用户管理PostgreSQL中,一个database下可以有多个schema。可以给schema指定一个owner,如果没有指定,那么当前用户就是schema的默认owner。
在Oracle数据库中不能直接新建一个schema,系统在创建一个用户的同时为这个用户创建一个同名的schem并作为该用户的缺省shcema。即schema的个数同user的个数相同,而且schema名字同user名字一一 对应并且相同。
在mysql中没有schema,所以创建一个database的效果和建立一个schema是相同的。我们可以简单的理解为,MySQL中的database就是schema。
?
创建一个用户
postgres=# create user sa with password sadmin;
CREATE ROLE
postgres=#

创建一个超级用户
postgres=# create user jdbwith password sadmin superuser;
CREATE ROLE
postgres=#

查看用户
postgres=#\\du
jdb| Superuser|
postgres| Superuser, Create role, Create DB, Replication, Bypass RLS |
sa||

修改用户密码
postgres=# alter user jdb with password oracle123;
ALTER ROLE
或者这样修改
postgres=# \\password sa
Enter new password for user "sa":
Enter it again:

将用户禁止登录
postgres=# alter user jdb with nologin;
ALTER ROLE

删除用户,首先要先将用户的对象删除,再删除用户
postgres=# drop owned by sa;
DROP OWNED
postgres=# \\du
jdb| Superuser, Cannot login|
postgres| Superuser, Create role, Create DB, Replication, Bypass RLS |
sa||

postgres=# drop user sa;
DROP ROLE

查看用户
postgres=# \\du
jdb| Superuser, Cannot login|
postgres| Superuser, Create role, Create DB, Replication, Bypass RLS |

模式一个数据库包含一个或多个命名模式,模式中包含着表。模式还包含其他类型的命名对象,包括数据类型、函数和操作符。
相同的对象名称可以被用于不同的模式中二不会出现冲突,例如schema1和myschema都可以包含名为mytable的表。和数据库不同,模式并不是被严格地隔离:一个用户可以访问他们所连接的数据库中的所有模式内的对象,只要他们有足够的权限。
              下面是一些使用方案的原因:
? 允许多个用户使用一个数据库并且不会互相干扰。
? 将数据库对象组织成逻辑组以便更容易管理。
? 第三方应用的对象可以放在独立的模式中,这样它们就不会与其他对象的名称发生冲突。
【pg快速入门--初步使用】模式类似于操作系统层的目录,但是模式不能嵌套。 
创建一个用户
postgres=# create role sa with password sadmin login;
CREATE ROLE

切换到mydb库
postgres=# \\c mydb
You are now connected to database "mydb" as user "postgres".

查看模式
mydb=# \\dn
public | postgres

创建和用户同名的 schema
mydb=#create schema sa authorization sa;
CREATE SCHEMA
mydb=# \\dn
public | postgres
sa| sa


在sa schema下创建一个表school:
mydb=# create table sa.school(
mydb(#IDINTNOT NULL,
mydb(#NAME VARCHAR (25)NOT NULL,
mydb(#AGEINTNOT NULL,
mydb(#ADDRESSCHAR (25),
mydb(#SALARYDECIMAL (20, 2

    推荐阅读