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        
 

基本操作

登入并创建数据库 

postgres@ubuntu:/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,了解到的一些概念在此。

下一篇将介绍更多逻辑结构。

猜你喜欢

转载自blog.csdn.net/jacicson1987/article/details/82703507