postgreSQL 常用命令 二
本次测试基与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 常用命令 二的更多相关文章
- [转] postgresql常用命令
PS: 数据库安装后,里面的每个数据库有自己的用户密码,需要dump的时候,指定用户pg_dump -U xxx <数据库> > 某个地址 最近一直在学习Postgresql,下面 ...
- linux初学 :linux 常用命令(二)
压缩和解压命令 gzip/guzip zip/unzip tar gzip和gunzip一般可用参数是-r,例: gzip test.txt 压缩文件 gzip -r test 压缩所有tes ...
- Git的基本原理与常用命令[二]
标签(linux): git 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 git 的四个区域 四种状态 常用命令 git add #加入暂存(索引区) git ...
- linux常用命令二
linux常用命令一 常用指令 ls 显示文件或目录 -l 列出文件详细信息l(list) -a 列出当前目录下所有文件及目录,包括隐藏的a(all ...
- linux(三)之linux常用命令二
今天就是星期五了,又可以休息两天了.有点小激动,开心.不过还是要加油,因为还有很多东西等着我去学习呢! 七.chmod 作用:修改文件的权限 7.1.命令格式:chmod mode filename ...
- postgresql常用命令
1.createdb 数据库名称 产生数据库2.dropdb 数据库名称 删除数据库 3.CREATE USER 用户名称 创建用户4.drop User 用户名称 删除用户 5.SELECT use ...
- postgresql 常用命令
普通用法: sudo su - postgres 切换到postgres用户下: psql -U user -d dbname 连接数据库, 默认的用户和数据库是postgres \c dbname ...
- Linux学习之常用命令(二)
1.上次介绍了一些常用的系统命令,这次又总结了一些小命令,故分享一下: 网卡地址查询的命令: ifconfig #不同于Windows系统,它的是ifconfig而不是ipconfig ip -a # ...
- Linux Linux常用命令二
whoami 我是谁命令 --该命令用户查看当前系统当前账号的用户名 --由于系统管理员通常需要使用多种身份登录系统,李儒通常使用普通用户登录系统,然后再以su命令切换到root身份对系统进行灌篮.这 ...
随机推荐
- 基于django rest framework做认证组件
先导入要用到的类 from rest_framework.authentication import BaseAuthentication from rest_framework.exceptions ...
- Servlet入门第一天
1. 使用 JavaEE 版的 Eclipse 开发动态的 WEB 工程(JavaWEB 项目) 1). 把开发选项切换到 JavaEE 2). 可以在 Window -> Show View ...
- cad转shapefile文件
private ESRI.ArcGIS.Controls.AxTOCControl axTOCControl1; private ESRI.ArcGIS.Controls.AxLicenseContr ...
- 树形DP-----HDU4003 Find Metal Mineral
Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Other ...
- Tomcat与Web.xml配置
1.编码配置 <Connector acceptCount=”100″ connectionTimeout=”20000″ disableUploadTimeout=”true” enableL ...
- DELPHI XE5轻松输出到MacOsX
配置:MACOSX10.9.3 +XCODE5.1 + VBOX + WINXP + DELPHI XE 5UP2 配置步骤从略. 1.选择firemonkey desktop application ...
- Newtonsoft.Json.Linq
var json = "{\"name\":\"ok1\",\"sex\":\"man\"}"; / ...
- Robot Framework 使用总结
最近项目上使用了RF快速实现了一些验收测试的自动化case,感觉不错,很好用,下面就记录一下使用RF实现自动化的过程. 什么是RF? RF是一种测试框架,帮助测试人员在其框架下快速实现验收测试的自动化 ...
- 打包发布到NPM并通过CDN访问
本文主要讲述基于webpack编写js包文件后上传到npm,并通过cdn进行访问. 创建项目 在自己新建的文件夹下执行如下代码: npm init name: (mtmap) version: (1. ...
- PHP7 - MongoDB Driver 使用心得
php7 只能使用Mongodb driver来驱动mongodb. 使用Mongodb Driver连接数据库 刚开始使用Mongodb Driver的时候我是拒绝的.查看官方文档只看到一排的类和不 ...