PostgreSQL學習第十篇 數據庫邏輯結構介紹--數據庫
來源:程序員人生 發布時間:2017-02-17 09:14:25 閱讀次數:6119次
PG中,數據的組織結構可以分為以下3層:
1. 數據庫:1個PostgreSQL數據庫服務下可以管理多個數據庫;
2. 表、索引:注:1般的PG中,表的術語叫relation,而其他數據庫中為table
3. 數據行:注:PG中行的術語1般為tuple,而在其他數據庫中則為row
在PostgreSQL中,1個數據庫服務(或叫實例)下可以有多個數據庫,而1個數據庫不能屬于多個實例。但是在Oracle中,1個實例只能有1個數據庫,但1個數據庫可以在多個實例中(rac)
數據庫基本操作:創建數據庫、刪除數據庫、修改數據庫等
創建數據庫:
create database db_name;
可以指定所屬用戶、模板(默許為templete1)、字符編碼,表空間等
修改數據庫:
alter database db_name with option;
可以修改連接限制、重命名數據庫、修改數據庫所屬用戶等
刪除數據庫:
drop database db_name;
示例:
postgres=# \l+
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description
-----------+----------+----------+-------------+-------------+-----------------------+---------+------------+--------------------------------------------
postgres | postgres | UTF8 | en_US.UTF⑻ | en_US.UTF⑻ | | 7359 kB | pg_default | default administrative connection database
template0 | postgres | UTF8 | en_US.UTF⑻ | en_US.UTF⑻ | =c/postgres +| 7225 kB | pg_default | unmodifiable empty database
| | | | | postgres=CTc/postgres | | |
template1 | postgres | UTF8 | en_US.UTF⑻ | en_US.UTF⑻ | =c/postgres +| 7225 kB | pg_default | default template for new databases
| | | | | postgres=CTc/postgres | | |
(3 rows)
postgres=# create database test owner=postgres template=template1 encoding=utf8 tablespace=pg_default connection limit=⑴;
CREATE DATABASE
postgres=# \l+
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description
-----------+----------+----------+-------------+-------------+-----------------------+---------+------------+--------------------------------------------
postgres | postgres | UTF8 | en_US.UTF⑻ | en_US.UTF⑻ | | 7359 kB | pg_default | default administrative connection database
template0 | postgres | UTF8 | en_US.UTF⑻ | en_US.UTF⑻ | =c/postgres +| 7225 kB | pg_default | unmodifiable empty database
| | | | | postgres=CTc/postgres | | |
template1 | postgres | UTF8 | en_US.UTF⑻ | en_US.UTF⑻ | =c/postgres +| 7225 kB | pg_default | default template for new databases
| | | | | postgres=CTc/postgres | | |
test | postgres | UTF8 | en_US.UTF⑻ | en_US.UTF⑻ | | 7225 kB | pg_default |
(4 rows)
postgres=# alter database test rename to testn;
ALTER DATABASE
postgres=# create tablespace test location '/home/postgres/test';
CREATE TABLESPACE
postgres=# alter database testn tablespace test;
ALTER DATABASE
postgres=# \l+
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description
-----------+----------+----------+-------------+-------------+-----------------------+---------+------------+--------------------------------------------
postgres | postgres | UTF8 | en_US.UTF⑻ | en_US.UTF⑻ | | 7359 kB | pg_default | default administrative connection database
template0 | postgres | UTF8 | en_US.UTF⑻ | en_US.UTF⑻ | =c/postgres +| 7225 kB | pg_default | unmodifiable empty database
| | | | | postgres=CTc/postgres | | |
template1 | postgres | UTF8 | en_US.UTF⑻ | en_US.UTF⑻ | =c/postgres +| 7225 kB | pg_default | default template for new databases
| | | | | postgres=CTc/postgres | | |
testn | postgres | UTF8 | en_US.UTF⑻ | en_US.UTF⑻ | | 7225 kB | test |
(4 rows)
postgres=#
postgres=# drop database test;
ERROR: database "test" does not exist
postgres=# drop database if exists test;
NOTICE: database "test" does not exist, skipping
DROP DATABASE
postgres=# drop database if exists testn;
postgres=# \l+
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description
-----------+----------+----------+-------------+-------------+-----------------------+---------+------------+--------------------------------------------
postgres | postgres | UTF8 | en_US.UTF⑻ | en_US.UTF⑻ | | 7359 kB | pg_default | default administrative connection database
template0 | postgres | UTF8 | en_US.UTF⑻ | en_US.UTF⑻ | =c/postgres +| 7225 kB | pg_default | unmodifiable empty database
| | | | | postgres=CTc/postgres | | |
template1 | postgres | UTF8 | en_US.UTF⑻ | en_US.UTF⑻ | =c/postgres +| 7225 kB | pg_default | default template for new databases
| | | | | postgres=CTc/postgres | | |
(3 rows)
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈