• 本次测试基与PostgreSQL 10.x版本

  • 创建用户

[postgres@rtm2 data]$ /opt/pgsql-10/bin/createuser rentaomin
[postgres@rtm2 data]$
  • 登陆psql查询创建的用户
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
rentaomin | | {} postgres=#
  • 创建数据库
[postgres@rtm2 data]$ /opt/pgsql-10/bin/createdb rmttest;
[postgres@rtm2 data]$
  • 查询创建的数据库
[postgres@rtm2 data]$ psql
psql (10.3)
Type "help" for help. postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
rmttest | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows) postgres=#
  • PostgreSQL 创建数据库名称第一个字符必须为 字母
postgres=# create database 12rt;
ERROR: syntax error at or near "12"
LINE 1: create database 12rt;
^
postgres=#
  • 数据库名称不能超过 63byte ,数据名称中间包含特殊字符,对于长于63字节的会 自动删除 多出的字节保留剩下的作为数据库名称
postgres=# create database qw12dd;
CREATE DATABASE
postgres=# create database rentaominrentaominrentaominrentamdsfsdfsdfsdfsdfsfsdfsdf;
CREATE DATABASE
postgres=# create database rentaominrentaominrentaominrentamdsfsdfsdfsdfsdfsfsdfsdfsdfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffwewerwerwerwerewrewrwer;
NOTICE: identifier "rentaominrentaominrentaominrentamdsfsdfsdfsdfsdfsfsdfsdfsdfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffwewerwerwerwerewrewrwer" will be truncated to "rentaominrentaominrentaominrentamdsfsdfsdfsdfsdfsfsdfsdfsdfffff"
CREATE DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------------------------------------------------------------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
qw12dd | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
rentaominrentaominrentaominrentamdsfsdfsdfsdfsdfsfsdfsdf | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
rentaominrentaominrentaominrentamdsfsdfsdfsdfsdfsfsdfsdfsdfffff | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(6 rows) postgres=#
  • 查看系统版本
postgres=# select version();
version
---------------------------------------------------------------------------------------------------------
PostgreSQL 10.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-16), 64-bit
(1 row) postgres=# ^C
postgres=#
  • psql命令行 CREATE TABLE,跨行时直接按 ENTER 键或者 \ 表示换行
test=# create table s (
test(# id serial,
test(# age int ,
test(# date timestamp default now()
test(# );
CREATE TABLE
test=# \d+ s
Table "public.s"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+-----------------------------+-----------+----------+-------------------------------+---------+--------------+-------------
id | integer | | not null | nextval('s_id_seq'::regclass) | plain | |
age | integer | | | | plain | |
date | timestamp without time zone | | | now() | plain | | test=#
  • psql 中的 real 数据类型,用于存储单精度的浮点数(32位浮点数)

  • psql 除支持标准的sql数据类型外,还可以自定义任意数量的数据类型,类型名称不能为关键字,除非要求支持sql标准的特殊情况。

  • 数据类型为 point

test=# create table s (
test(# id serial,
test(# age int ,
test(# date timestamp default now()
test(# );
CREATE TABLE
  • 插入数据,坐标必须用 单括号 包起来
test=# insert into cities values ('北京','(-192.0,53.0)');
INSERT 0 1
test=# select * from cities ;
name | location
------+-----------
北京 | (-192,53)
(1 row) test=#
  • character varying(80) 数据类型与varchar(80) 相同,postgreSQL 默认创建的表名为 小写

  • 外键约束

test=# create table city (
test(# cityName varchar(80) primary key,
test(# location point
test(# );
CREATE TABLE test=# create table weather (
city varchar(80) references city (cityName),
temp_lo int ,
temp_hi int , -- hight temratory
prcp real ,
date date
);
CREATE TABLE
  • 若直接向 weather 插入数据,则会报错
test=# insert into weather values('湖南',13,34,0.12345678,'2018-05-20');
ERROR: insert or update on table "weather" violates foreign key constraint "weather_city_fkey"
DETAIL: Key (city)=(湖南) is not present in table "city".
  • 必须先 city 表插入数据,才能向 weather 插入数据
test=# insert into city values ('湖南','(-192.0,45)');
INSERT 0 1
test=# select * from city;
cityname | location
----------+-----------
湖南 | (-192,45)
(1 row) test=# insert into weather values('湖南',13,34,0.12345678,'2018-05-20');
INSERT 0 1
test=# select * from weather ;
city | temp_lo | temp_hi | prcp | date
------+---------+---------+----------+------------
湖南 | 13 | 34 | 0.123457 | 2018-05-20
(1 row) test=#
  • 注意 ,此处在插入的数据类型为real的数据实际为0.12345678,但是在表中实际的数据为0.1234567,是由于 real为单精度浮点数类型(32位浮点数)

  • 显示声明事务块

BEGIN; UPDATE accounts SET balance = balance - 100.00    WHERE name = 'Alice'; -- etc etc COMMIT;
  • 同时删除多张表
test=# \d+
List of relations
Schema | Name | Type | Owner | Size | Description
--------+-----------+----------+----------+------------+-------------
public | cities | table | postgres | 8192 bytes |
public | city | table | postgres | 8192 bytes |
public | p | table | postgres | 16 kB |
public | p_id_seq | sequence | postgres | 8192 bytes |
public | s | table | postgres | 0 bytes |
public | s_id_seq | sequence | postgres | 8192 bytes |
public | t | table | postgres | 8192 bytes |
public | t2 | table | postgres | 0 bytes |
public | t2_id_seq | sequence | postgres | 8192 bytes |
public | t_id_seq | sequence | postgres | 8192 bytes |
public | userinfo | view | postgres | 0 bytes |
public | weather | table | postgres | 8192 bytes |
(12 rows)
test=# drop table cities, t2, t;
DROP TABLE
  • 创建继承表,PostgreSQL 中表可以 继承多张表
test=# create table person (id serial ,name varchar(80),age int );
CREATE TABLE
test=# create table sex (state char(1)) inherits (person);
CREATE TABLE test=# insert into sex (name,age,state) values ('张三',22,1);
INSERT 0 1
test=# insert into sex (name,age,state) values ('李四',23,0);
INSERT 0 1 test=# insert into sex (name,age,state) values ('成杰',23,0);
INSERT 0 1
test=# insert into sex (name,age,state) values ('李文杰',23,0);
INSERT 0 1
test=# insert into sex (name,age,state) values ('王麻子',25,1);
INSERT 0 1
  • 查询表,其中 Only支持SELECT,UPDATE,DELETE;
test=# select * from sex ;
id | name | age | state
----+--------+-----+-------
5 | 成杰 | 23 | 0
6 | 李文杰 | 23 | 0
7 | 王麻子 | 25 | 1
(3 rows) test=# select * from person;
id | name | age
----+--------+-----
1 | 张三 | 22
2 | 李四 | 23
5 | 成杰 | 23
6 | 李文杰 | 23
7 | 王麻子 | 25
(5 rows) test=# select * from only person;
id | name | age
----+------+-----
1 | 张三 | 22
2 | 李四 | 23
(2 rows) test=#
  • zlib 包用于支持 pg_dump and pg_restore.

postgreSQL 常用命令 二的更多相关文章

  1. [转] postgresql常用命令

    PS: 数据库安装后,里面的每个数据库有自己的用户密码,需要dump的时候,指定用户pg_dump -U xxx <数据库>  > 某个地址 最近一直在学习Postgresql,下面 ...

  2. linux初学 :linux 常用命令(二)

    压缩和解压命令 gzip/guzip   zip/unzip   tar gzip和gunzip一般可用参数是-r,例: gzip test.txt 压缩文件 gzip -r test 压缩所有tes ...

  3. Git的基本原理与常用命令[二]

    标签(linux): git 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 git 的四个区域 四种状态 常用命令 git add #加入暂存(索引区) git ...

  4. linux常用命令二

    linux常用命令一 常用指令 ls        显示文件或目录 -l           列出文件详细信息l(list) -a          列出当前目录下所有文件及目录,包括隐藏的a(all ...

  5. linux(三)之linux常用命令二

    今天就是星期五了,又可以休息两天了.有点小激动,开心.不过还是要加油,因为还有很多东西等着我去学习呢! 七.chmod 作用:修改文件的权限 7.1.命令格式:chmod mode filename ...

  6. postgresql常用命令

    1.createdb 数据库名称 产生数据库2.dropdb 数据库名称 删除数据库 3.CREATE USER 用户名称 创建用户4.drop User 用户名称 删除用户 5.SELECT use ...

  7. postgresql 常用命令

    普通用法: sudo su - postgres 切换到postgres用户下: psql -U user -d dbname 连接数据库, 默认的用户和数据库是postgres \c dbname ...

  8. Linux学习之常用命令(二)

    1.上次介绍了一些常用的系统命令,这次又总结了一些小命令,故分享一下: 网卡地址查询的命令: ifconfig #不同于Windows系统,它的是ifconfig而不是ipconfig ip -a # ...

  9. Linux Linux常用命令二

    whoami 我是谁命令 --该命令用户查看当前系统当前账号的用户名 --由于系统管理员通常需要使用多种身份登录系统,李儒通常使用普通用户登录系统,然后再以su命令切换到root身份对系统进行灌篮.这 ...

随机推荐

  1. Luogu 4841 城市规划

    BZOJ 3456 权限题 太菜了推不出式子 我们设$f(n)$表示$n$个点的无向连通图的数量,那么有 $$f(n) = 2^{\binom{n}{2}} - \sum_{i = 1}^{n - 1 ...

  2. Part7-时钟初始化_lesson1

    1.概念解析 1.1时钟脉冲信号 1.2时钟脉冲频率 1.3时钟源(提供时钟脉冲信号) a.晶振 b.锁相环PLL 2.时钟体系 2440: 晶振的频率.时钟体系有多少个PLL.这些PLL分别产生了哪 ...

  3. (11)Web程序保存状态的几种方式,Application,Session,Cookie,ViewState

    WEb程序保存状态的方式有这样几种: 1.Application:保存在Application中的数据是全局有效的:Application里面存放的应该是访问多修      改较少并且是全局至少大部分 ...

  4. python 单双引号交替的json串

    单双引号交替的json串 1.常见的json串,类似于这种{"isSucess":true, "name":"yoyo", "st ...

  5. asp.net 类,接口

    ASP.NET抽象类和接口的比较 相同点 ●都不能被直接实例化,都可以通过继承实现其抽象方法. ●都是面向抽象编程的技术基础,实现了诸多的设计模式. 不同点 ●接口支持多继承:抽象类不能实现多继承.  ...

  6. 关于C#中Timer定时器的重入问题解决方法(也适用于多线程)

    项目中用到了定时器随着服务启动作定时任务,按指定的准点时间定时执行相关操作,但是在指定准点时间内我只想让它执行一次,要避免重入问题的发生. 首先简单介绍一下timer,这里所说的timer是指的Sys ...

  7. CI框架集成Smarty

    1.下载smarty源码包,解压放置于项目目录 libriaries中 2.在libraries中建立Cismarty.php ,填写如下代码 <?php if(!defined('BASEPA ...

  8. CPU 和 GPU 的区别

    作者:知乎用户链接:https://www.zhihu.com/question/19903344/answer/96081382来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  9. AppIcon应用图标 and Launchimage启动图标的制作

    1.制作软件 需要在AppStore里搜索:Appicons and Launchimages Lite 2.操作步骤 看图示意(三步) 1)选择资源源文件 2)选择需要应用的平台 3)选择生成的目标 ...

  10. Mysql初识数据库《三》数据库概述

    1 什么是数据(Data) 描述事物的符号记录称为数据,描述事物的符号既可以是数字,也可以是文字.图片,图像.声音.语言等,数据由多种表现形式,它们都可以经过数字化后存入计算机 在计算机中描述一个事物 ...