1. 程式人生 > >postgreSQL 常用命令 二

postgreSQL 常用命令 二

  • 本次測試基與PostgreSQL 10.x版本

  • 建立使用者

[[email protected] data]$ /opt/pgsql-10/bin/createuser rentaomin
[[email protected] data]$
  • 登陸psql查詢建立的使用者
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} rentaomin | | {} postgres=#
  • 建立資料庫
[[email protected] data]$ /opt/pgsql-10/bin/createdb rmttest;
[[email protected] data]$
  • 查詢建立的資料庫
[[email protected]
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.