1. 程式人生 > >postgresql基礎學習(一)——基本命令和部分邏輯結構

postgresql基礎學習(一)——基本命令和部分邏輯結構

目錄

 

安裝和配置

基本操作

邏輯結構

結構簡圖

結構說明

schema操作

小結:


安裝和配置

PostGresql ubuntu安裝:

apt-get install postgresql

service postgresql start

資料庫目錄所在
/var/lib/postgresql/9.5/main

配置檔案目錄所在
/etc/postgresql/9.5/main/postgresql.conf

修改log配置
log_directory = 'pg_log'            

log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'               
log_truncate_on_rotation = off         
log_rotation_age = 1d                  
log_rotation_size = 0        
 

基本操作

登入並建立資料庫 

[email protected]:/root$ psql
could not change directory to "/root": Permission denied
psql (9.5.14)
Type "help" for help.

postgres=# 
postgres=# 
postgres=# create database testdb;
CREATE DATABASE

切換資料庫

postgres=# \c testdb
You are now connected to database "testdb" as user "postgres".
testdb=# 

檢視資料庫

testdb=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 fit       | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 testdb    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(5 rows)

檢視該資料庫下所有表

testdb=# \d
        List of relations
 Schema | Name | Type  |  Owner   
--------+------+-------+----------
 public | t1   | table | postgres
(1 row)

查看錶結構

testdb=# \d t1
             Table "public.t1"
 Column |         Type          | Modifiers 
--------+-----------------------+-----------
 id     | integer               | not null
 name   | character varying(40) | 
Indexes:
    "t1_pkey" PRIMARY KEY, btree (id)

查看錶索引

testdb=# \d t1_pkey
    Index "public.t1_pkey"
 Column |  Type   | Definition 
--------+---------+------------
 id     | integer | id
primary key, btree, for table "public.t1"

\d+ 萬用字元

testdb=# \d t?
             Table "public.t1"
 Column |         Type          | Modifiers 
--------+-----------------------+-----------
 id     | integer               | not null
 name   | character varying(40) | 
Indexes:
    "t1_pkey" PRIMARY KEY, btree (id)


testdb=# \d *pkey
    Index "public.t1_pkey"
 Column |  Type   | Definition 
--------+---------+------------
 id     | integer | id
primary key, btree, for table "public.t1"

testdb=# \d t*
             Table "public.t1"
 Column |         Type          | Modifiers 
--------+-----------------------+-----------
 id     | integer               | not null
 name   | character varying(40) | 
Indexes:
    "t1_pkey" PRIMARY KEY, btree (id)

    Index "public.t1_pkey"
 Column |  Type   | Definition 
--------+---------+------------
 id     | integer | id
primary key, btree, for table "public.t1"

查看錶詳細資訊

testdb=# \d+ t1
                                 Table "public.t1"
 Column |         Type          | Modifiers | Storage  | Stats target | Description 
--------+-----------------------+-----------+----------+--------------+-------------
 id     | integer               | not null  | plain    |              | 
 name   | character varying(40) |           | extended |              | 
Indexes:
    "t1_pkey" PRIMARY KEY, btree (id)

檢視許可權分配

testdb=# \dp t1
                            Access privileges
 Schema | Name | Type  | Access privileges | Column privileges | Policies 
--------+------+-------+-------------------+-------------------+----------
 public | t1   | table |                   |                   | 
(1 row)

testdb=# 

修改日期格式

testdb=# show datestyle
testdb-# ;
 DateStyle 
-----------
 ISO, MDY
(1 row)
testdb=# set datestyle='YMD';
SET
testdb=# show datestyle;
 DateStyle 
-----------
 ISO, YMD
(1 row)

邏輯結構

結構簡圖

結構說明

postgresql一個例項可以有多個數據庫
應用連線到一個數據庫,不能訪問其他資料庫,除非dblink

ralation 相當於table

tuple 相當於 row

schema,模式,相當於一個名稱空間

schema包含:表,函式,操作符等物件,多個schema的物件可以重名,不衝突。

連線到資料庫後,可以訪問多個schema的物件。

schema操作

//建立模式
testdb=# create schema ckdba
testdb-# ;
CREATE SCHEMA
testdb=# 
testdb=# \dn
  List of schemas
  Name  |  Owner   
--------+----------
 ckdba  | postgres
 public | postgres
(2 rows)

//建立該模式下的表
testdb=# create table ckdba.t3(id int, postion text)
testdb-# ;
CREATE TABLE
testdb=# \d+ ckdba.
                           Table "ckdba.t3"
 Column  |  Type   | Modifiers | Storage  | Stats target | Description 
---------+---------+-----------+----------+--------------+-------------
 id      | integer |           | plain    |              | 
 postion | text    |           | extended |              | 

//刪除模式及其下的表
testdb=# drop schema ckdba CASCADE;
NOTICE:  drop cascades to table ckdba.t3
DROP SCHEMA
testdb=# 
testdb=# \dn
  List of schemas
  Name  |  Owner   
--------+----------
 public | postgres
(1 row)

小結:

初識PG,瞭解到的一些概念在此。

下一篇將介紹更多邏輯結構。