個人求職網(wǎng)站如何做官網(wǎng)站內(nèi)推廣內(nèi)容
數(shù)據(jù)庫
創(chuàng)建數(shù)據(jù)庫
PostgreSQL 創(chuàng)建數(shù)據(jù)庫可以用以下三種方式:
- 1、使用 CREATE DATABASE SQL 語句來創(chuàng)建。
- 2、使用 createdb 命令來創(chuàng)建。
- 3、使用 pgAdmin 工具。
CREATE DATABASE 創(chuàng)建數(shù)據(jù)庫
CREATE DATABASE 命令需要在 PostgreSQL 命令窗口來執(zhí)行,語法格式如下:
postgres=# \h create database;
Command: CREATE DATABASE
Description: create a new database
Syntax:
CREATE DATABASE name[ [ WITH ] [ OWNER [=] user_name ][ TEMPLATE [=] template ][ ENCODING [=] encoding ][ LOCALE [=] locale ][ LC_COLLATE [=] lc_collate ][ LC_CTYPE [=] lc_ctype ][ TABLESPACE [=] tablespace_name ][ ALLOW_CONNECTIONS [=] allowconn ][ CONNECTION LIMIT [=] connlimit ][ IS_TEMPLATE [=] istemplate ] ]URL: https://www.postgresql.org/docs/13/sql-createdatabase.html
例如,我們創(chuàng)建一個 lhrpgdb 的數(shù)據(jù)庫:
postgres=# CREATE DATABASE lhrpgdb;CREATE DATABASE db1WITHOWNER = lhrENCODING = 'UTF8'TABLESPACE = ts_test1CONNECTION LIMIT = -1;
createdb 命令創(chuàng)建數(shù)據(jù)庫
createdb 是一個 SQL 命令 CREATE DATABASE 的封裝。
createdb 命令語法格式如下:
createdb [option...] [dbname [description]]
參數(shù)說明:
dbname:要創(chuàng)建的數(shù)據(jù)庫名。
description:關于新創(chuàng)建的數(shù)據(jù)庫相關的說明。
options:參數(shù)可選項,可以是以下值:
序號 | 選項 & 描述 |
---|---|
1 | -D tablespace指定數(shù)據(jù)庫默認表空間。 |
2 | -e將 createdb 生成的命令發(fā)送到服務端。 |
3 | -E encoding指定數(shù)據(jù)庫的編碼。 |
4 | -l locale指定數(shù)據(jù)庫的語言環(huán)境。 |
5 | -T template指定創(chuàng)建此數(shù)據(jù)庫的模板。 |
6 | –help顯示 createdb 命令的幫助信息。 |
7 | -h host指定服務器的主機名。 |
8 | -p port指定服務器監(jiān)聽的端口,或者 socket 文件。 |
9 | -U username連接數(shù)據(jù)庫的用戶名。 |
10 | -w忽略輸入密碼。 |
11 | -W連接時強制要求輸入密碼。 |
接下來我們打開一個命令窗口,進入到 PostgreSQL 的安裝目錄,并進入到 bin 目錄,createdb 命令位于 PostgreSQL安裝目錄/bin 下,執(zhí)行創(chuàng)建數(shù)據(jù)庫的命令:
$ cd /Library/PostgreSQL/11/bin/
$ createdb -h localhost -p 5432 -U postgres lhrpgdb
password ******
以上命令我們使用了超級用戶 postgres 登錄到主機地址為 localhost,端口號為 5432 的 PostgreSQL 數(shù)據(jù)庫中并創(chuàng)建 lhrpgdb 數(shù)據(jù)庫。
pgAdmin 工具創(chuàng)建數(shù)據(jù)庫
pgAdmin 工具提供了完整操作數(shù)據(jù)庫的功能:
選擇數(shù)據(jù)庫
上一章節(jié)我們講了如何創(chuàng)建數(shù)據(jù)庫,接下來我們來討論如何去選擇我們創(chuàng)建的數(shù)據(jù)庫。
數(shù)據(jù)庫的命令窗口
PostgreSQL 命令窗口中,我們可以命令提示符后面輸入 SQL 語句:
postgres=#
使用 \l 用于查看已經(jīng)存在的數(shù)據(jù)庫:
postgres=# \lList of databasesName | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+---------+-------+-----------------------postgres | postgres | UTF8 | C | C | lhrpgdb | postgres | UTF8 | C | C | template0 | postgres | UTF8 | C | C | =c/postgres +| | | | | postgres=CTc/postgrestemplate1 | postgres | UTF8 | C | C | =c/postgres +| | | | | postgres=CTc/postgres
(4 rows)
接下來我們可以使用 \c + 數(shù)據(jù)庫名 來進入數(shù)據(jù)庫:
postgres=# \c lhrpgdb
You are now connected to database "lhrpgdb" as user "postgres".
lhrpgdb=#
lhrpgdb=# select current_user,current_database(),pg_backend_pid();current_user | current_database | pg_backend_pid
--------------+------------------+----------------postgres | lhrpgdb | 2715
(1 row)
系統(tǒng)命令行窗口
在系統(tǒng)的命令行查看,我么可以在連接數(shù)據(jù)庫后面添加數(shù)據(jù)庫名來選擇數(shù)據(jù)庫:
$ psql -h localhost -p 5432 -U postgress lhrpgdb
Password for user postgress: ****
psql (11.3)
Type "help" for help.
You are now connected to database "lhrpgdb" as user "postgres".
lhrpgdb=#
pgAdmin 工具
pgAdmin 工具更簡單了,直接點擊數(shù)據(jù)庫選擇就好了,還可以查看一些數(shù)據(jù)庫額外的信息:
刪除數(shù)據(jù)庫
PostgreSQL 刪除數(shù)據(jù)庫可以用以下三種方式:
- 1、使用 DROP DATABASE SQL 語句來刪除。
- 2、使用 dropdb 命令來刪除。
- 3、使用 pgAdmin 工具。
**注意:**刪除數(shù)據(jù)庫要謹慎操作,一旦刪除,所有信息都會消失。
DROP DATABASE 刪除數(shù)據(jù)庫
DROP DATABASE 會刪除數(shù)據(jù)庫的系統(tǒng)目錄項并且刪除包含數(shù)據(jù)的文件目錄。
DROP DATABASE 只能由超級管理員或數(shù)據(jù)庫擁有者執(zhí)行。
DROP DATABASE 命令需要在 PostgreSQL 命令窗口來執(zhí)行,語法格式如下:
DROP DATABASE [ IF EXISTS ] name
參數(shù)說明:
- IF EXISTS:如果數(shù)據(jù)庫不存在則發(fā)出提示信息,而不是錯誤信息。
- name:要刪除的數(shù)據(jù)庫的名稱。
例如,我們刪除一個 lhrpgdb 的數(shù)據(jù)庫:
postgres=# DROP DATABASE lhrpgdb;
dropdb 命令刪除數(shù)據(jù)庫
dropdb 是 DROP DATABASE 的包裝器。
dropdb 用于刪除 PostgreSQL 數(shù)據(jù)庫。
dropdb 命令只能由超級管理員或數(shù)據(jù)庫擁有者執(zhí)行。
dropdb 命令語法格式如下:
dropdb [connection-option...] [option...] dbname
參數(shù)說明:
dbname:要刪除的數(shù)據(jù)庫名。
options:參數(shù)可選項,可以是以下值:
序號 | 選項 & 描述 |
---|---|
1 | -e顯示 dropdb 生成的命令并發(fā)送到數(shù)據(jù)庫服務器。 |
2 | -i在做刪除的工作之前發(fā)出一個驗證提示。 |
3 | -V打印 dropdb 版本并退出。 |
4 | –if-exists如果數(shù)據(jù)庫不存在則發(fā)出提示信息,而不是錯誤信息。 |
5 | –help顯示有關 dropdb 命令的幫助信息。 |
6 | -h host指定運行服務器的主機名。 |
7 | -p port指定服務器監(jiān)聽的端口,或者 socket 文件。 |
8 | -U username連接數(shù)據(jù)庫的用戶名。 |
9 | -w連接數(shù)據(jù)庫的用戶名。 |
10 | -W連接時強制要求輸入密碼。 |
11 | –maintenance-db=dbname刪除數(shù)據(jù)庫時指定連接的數(shù)據(jù)庫,默認為 postgres,如果它不存在則使用 template1。 |
接下來我們打開一個命令窗口,進入到 PostgreSQL 的安裝目錄,并進入到 bin 目錄,dropdb 名位于 PostgreSQL安裝目錄/bin 下,執(zhí)行刪除數(shù)據(jù)庫的命令:
$ cd /Library/PostgreSQL/11/bin/
$ dropdb -h localhost -p 5432 -U postgres lhrpgdb
password ******
以上命令我們使用了超級用戶 postgres 登錄到主機地址為 localhost,端口號為 5432 的 PostgreSQL 數(shù)據(jù)庫中并刪除 lhrpgdb 數(shù)據(jù)庫。
pgAdmin 工具刪除據(jù)庫
pgAdmin 工具提供了完整操作數(shù)據(jù)庫的功能:
查詢
-- \l+SELECT d.datname as "Name",pg_catalog.pg_get_userbyid(d.datdba) as "Owner",pg_catalog.pg_encoding_to_char(d.encoding) as "Encoding",d.datcollate as "Collate",d.datctype as "Ctype",pg_catalog.array_to_string(d.datacl, E'\n') AS "Access privileges",CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))ELSE 'No Access'END as "Size",t.spcname as "Tablespace",pg_catalog.shobj_description(d.oid, 'pg_database') as "Description"
FROM pg_catalog.pg_database dJOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid
ORDER BY 1;-- 查看各數(shù)據(jù)庫數(shù)據(jù)創(chuàng)建時間
SELECTdatname,(pg_stat_file (format ( '%s/%s/PG_VERSION', CASE WHEN spcname = 'pg_default' THEN 'base' ELSE'pg_tblspc/' || t2.oid || '/PG_11_201804061/' END, t1.oid ))).*
FROMpg_database t1,pg_tablespace t2
WHEREt1.dattablespace = t2.oid;
表空間
用戶必須有表空間所在目錄訪問權(quán)限,所以在創(chuàng)建表空間之前需要在對應分區(qū)下創(chuàng)建相應的目錄,并為其分配權(quán)限。
PostgreSQL中的表空間允許在文件系統(tǒng)中定義用來存放表示數(shù)據(jù)庫對象的文件的位置。在PostgreSQL中表空間實際上就是給表指定一個存儲目錄,能合理利用磁盤性能和空間,制定最優(yōu)的物理存儲方式來管理數(shù)據(jù)庫表和索引。
在DB2和Oracle數(shù)據(jù)庫中;一個表空間只屬于一個數(shù)據(jù)庫使用;而一個數(shù)據(jù)庫可以擁有多個表空間。屬于"一對多"的關系。
在PostgreSQL集群中;一個表空間可以讓多個數(shù)據(jù)庫使用;而一個數(shù)據(jù)庫可以使用多個表空間。屬于"多對多"的關系。用戶下面擁有表,擁有模式。模式下面擁有表空間。
- initdb()后馬上創(chuàng)建pg_default和pg_global表空間
- 建表時如果沒有指定特定的表空間,表默認被存在pg_default表空間中。
- 用于管理整個數(shù)據(jù)庫集群的表默認被存儲在pg_global表空間中。
- pg_default表空間的物理位置為$PGDATA\base目錄。
- pg_global表空間的物理位置為$PGDATA\global目錄。
- 一個表空間可以被多個數(shù)據(jù)庫同時使用。此時,每一個數(shù)據(jù)庫都會在表空間路徑下創(chuàng)建為一個新的子路徑。
- 創(chuàng)建一個用戶表空間會在$PGDATA\pg_tblspc目錄下面創(chuàng)建一個軟連接,連接到表空間制定的目錄位置。
目錄結(jié)構(gòu)可以用大致下面圖示意:
創(chuàng)建表空間
mkdir -p /postgresql/pgdata/ts_test1
mkdir -p /postgresql/pgdata/ts_test2psql
\h create tablespace
create tablespace ts_test1 location '/postgresql/pgdata/ts_test1';
create tablespace ts_test2 location '/postgresql/pgdata/ts_test2';
\db
create table lhrtest (id int) tablespace ts_test1;
\d+ lhrtest
alter table lhrtest set tablespace ts_test2;select pg_relation_filepath('lhrtest');
執(zhí)行過程:
[pgsql@lhrpg pgdata]$ mkdir -p /postgresql/pgdata/ts_test1
[pgsql@lhrpg pgdata]$ mkdir -p /postgresql/pgdata/ts_test2
[pgsql@lhrpg pgdata]$ psql
psql (13.2)
Type "help" for help.postgres=# \h create tablespace;
Command: CREATE TABLESPACE
Description: define a new tablespace
Syntax:
CREATE TABLESPACE tablespace_name[ OWNER { new_owner | CURRENT_USER | SESSION_USER } ]LOCATION 'directory'[ WITH ( tablespace_option = value [, ... ] ) ]URL: https://www.postgresql.org/docs/13/sql-createtablespace.htmlpostgres=# \db+List of tablespacesName | Owner | Location | Access privileges | Options | Size | Description
------------+----------+----------+-------------------+---------+--------+-------------pg_default | postgres | | | | 295 MB | pg_global | postgres | | | | 559 kB |
(2 rows)postgres=#
postgres=# create tablespace ts_test1 location '/postgresql/pgdata/ts_test1';
WARNING: tablespace location should not be inside the data directory
CREATE TABLESPACE
postgres=# create tablespace ts_test2 location '/postgresql/pgdata/ts_test2';
WARNING: tablespace location should not be inside the data directory
CREATE TABLESPACE
postgres=# \dbList of tablespacesName | Owner | Location
------------+----------+-----------------------------pg_default | postgres | pg_global | postgres | ts_test1 | postgres | /postgresql/pgdata/ts_test1ts_test2 | postgres | /postgresql/pgdata/ts_test2
(4 rows)postgres=#
postgres=# create table lhrtest (id int) tablespace ts_test1;
CREATE TABLE
postgres=# \d+ lhrtestTable "public.lhrtest"Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+---------+-----------+----------+---------+---------+--------------+-------------id | integer | | | | plain | |
Tablespace: "ts_test1"
Access method: heappostgres=#
postgres=# alter table lhrtest set tablespace ts_test2;
ALTER TABLE
postgres=# \d+ lhrtestTable "public.lhrtest"Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+---------+-----------+----------+---------+---------+--------------+-------------id | integer | | | | plain | |
Tablespace: "ts_test2"
Access method: heappostgres=# \dList of relationsSchema | Name | Type | Owner
--------+---------+-------+----------public | lhrtest | table | postgrespublic | sbtest | table | lhr2
(2 rows)[pgsql@lhrpg pgdata]$ ll pg_tblspc/
total 0
lrwxrwxrwx 1 pgsql pgsql 27 May 28 09:13 16534 -> /postgresql/pgdata/ts_test1
lrwxrwxrwx 1 pgsql pgsql 27 May 28 09:21 16535 -> /postgresql/pgdata/ts_test2
刪除表空間
查詢
SELECT oid,spcname AS "Name",pg_catalog.pg_get_userbyid(spcowner) AS "Owner",pg_catalog.pg_tablespace_location(oid) AS "Location",pg_catalog.array_to_string(spcacl, E'\n') AS "Access privileges",spcoptions AS "Options",pg_catalog.pg_size_pretty(pg_catalog.pg_tablespace_size(oid)) AS "Size",pg_catalog.shobj_description(oid, 'pg_tablespace') AS "Description"
FROM pg_catalog.pg_tablespace
ORDER BY 1;\db+
用戶和角色
用戶和角色在PostgreSQL中是一個概念。 但是, CREATE ROLE創(chuàng)建的用戶默認不帶LOGIN屬性,而CREATE USER創(chuàng)建的用戶默認帶有LOGIN屬性。如果給role授權(quán)l(xiāng)ogin則等同user。
創(chuàng)建用戶
create user lhr with password 'lhr';
GRANT ALL PRIVILEGES ON DATABASE postgres to lhr;CREATE USER lhr2 WITH
LOGIN SUPERUSER CREATEDB CREATEROLE
INHERIT REPLICATION
CONNECTION LIMIT -1
PASSWORD 'lhr';CREATE ROLE username WITH LOGIN password 'l';
ALTER ROLE username WITH NOLOGIN;
ALTER ROLE username WITH login;
查:
postgres=# \du+List of rolesRole name | Attributes | Member of | Description
-----------+------------------------------------------------------------+-----------+-------------lhr | | {} |lhr2 | Superuser, Create role, Create DB, Replication | {} |postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {} |postgres=# select * from pg_user;usename | usesysid | usecreatedb | usesuper | userepl | usebypassrls | passwd | valuntil | useconfig
----------+----------+-------------+----------+---------+--------------+----------+----------+-----------postgres | 10 | t | t | t | t | ******** | |lhr | 16540 | f | f | f | f | ******** | |lhr2 | 16541 | t | t | t | f | ******** | |
(3 rows)
刪除用戶
-- ERROR: role "lhr" cannot be dropped because some objects depend on it
drop owned by lhr cascade;-- 若有數(shù)據(jù)庫,那么還需要刪掉數(shù)據(jù)庫
drop user lhr;
執(zhí)行:
sbtest=# drop user lhr ;
ERROR: role "lhr" cannot be dropped because some objects depend on it
DETAIL: privileges for database postgres
sbtest=# drop owned by lhr cascade;
DROP OWNED
sbtest=# drop user lhr ;
DROP ROLE
查詢
\du+
select * from pg_user;
模式(schema)
模式(schema):我們在pg數(shù)據(jù)庫中創(chuàng)建的任何對象(表,索引,視圖等)都會在一個模式下被創(chuàng)建。
當創(chuàng)建對象時,如果未指定模式,這些對象將會在默認的模式下被創(chuàng)建.這個模式叫做public。public模式,代表所有人的意思。 一個例外情況是另一個模式首先出現(xiàn)在search_path中。
PostgreSQL 模式(SCHEMA)可以看著是一個表的集合。
一個模式可以包含視圖、索引、數(shù)據(jù)類型、函數(shù)和操作符等。
相同的對象名稱可以被用于不同的模式中而不會出現(xiàn)沖突,例如 schema1 和 myschema 都可以包含名為 mytable 的表。
使用模式的優(yōu)勢:
- 允許多個用戶使用一個數(shù)據(jù)庫并且不會互相干擾。
- 將數(shù)據(jù)庫對象組織成邏輯組以便更容易管理。
- 第三方應用的對象可以放在獨立的模式中,這樣它們就不會與其他對象的名稱發(fā)生沖突。
模式類似于操作系統(tǒng)層的目錄,但是模式不能嵌套。
創(chuàng)建SCHEMA
我們可以使用 CREATE SCHEMA 語句來創(chuàng)建模式,語法格式如下:
postgres=# \h create schema;
Command: CREATE SCHEMA
Description: define a new schema
Syntax:
CREATE SCHEMA schema_name [ AUTHORIZATION role_specification ] [ schema_element [ ... ] ]
CREATE SCHEMA AUTHORIZATION role_specification [ schema_element [ ... ] ]
CREATE SCHEMA IF NOT EXISTS schema_name [ AUTHORIZATION role_specification ]
CREATE SCHEMA IF NOT EXISTS AUTHORIZATION role_specificationwhere role_specification can be:user_name| CURRENT_USER| SESSION_USERURL: https://www.postgresql.org/docs/13/sql-createschema.html
接下來我們連接到 lhrpgdb 來創(chuàng)建模式 myschema:
lhrpgdb=# create schema myschema;
CREATE SCHEMAlhrpgdb=# \dnList of schemasName | Owner
----------+----------lhr | postgresmyschema | postgrespublic | postgres
(3 rows)lhrpgdb=# \dnSList of schemasName | Owner
--------------------+----------information_schema | postgreslhr | postgresmyschema | postgrespg_catalog | postgrespg_temp_1 | postgrespg_toast | postgrespg_toast_temp_1 | postgrespublic | postgres
(8 rows)lhrpgdb=# \dnS+List of schemasName | Owner | Access privileges | Description
--------------------+----------+----------------------+----------------------------------information_schema | postgres | postgres=UC/postgres+|| | =U/postgres |lhr | postgres | |myschema | postgres | |pg_catalog | postgres | postgres=UC/postgres+| system catalog schema| | =U/postgres |pg_temp_1 | postgres | |pg_toast | postgres | | reserved schema for TOAST tablespg_toast_temp_1 | postgres | |public | postgres | postgres=UC/postgres+| standard public schema| | =UC/postgres |
(8 rows)
輸出結(jié)果 “CREATE SCHEMA” 就代表模式創(chuàng)建成功。
接下來我們再創(chuàng)建一個表格:
lhrpgdb=# create table myschema.company(ID INT NOT NULL,NAME VARCHAR (20) NOT NULL,AGE INT NOT NULL,ADDRESS CHAR (25),SALARY DECIMAL (18, 2),PRIMARY KEY (ID)
);
以上命令創(chuàng)建了一個空的表格,我們使用以下 SQL 來查看表格是否創(chuàng)建:
lhrpgdb=# select * from myschema.company;id | name | age | address | salary
----+------+-----+---------+--------
(0 rows)
刪除模式
刪除一個為空的模式(其中的所有對象已經(jīng)被刪除):
DROP SCHEMA myschema;
刪除一個模式以及其中包含的所有對象:
DROP SCHEMA myschema CASCADE;
查詢
select * from pg_catalog.pg_namespace;SELECT n.nspname AS "Name",pg_catalog.pg_get_userbyid(n.nspowner) AS "Owner",pg_catalog.array_to_string(n.nspacl, E'\n') AS "Access privileges",pg_catalog.obj_description(n.oid, 'pg_namespace') AS "Description"
FROM pg_catalog.pg_namespace n
ORDER BY 1;
表格,我們使用以下 SQL 來查看表格是否創(chuàng)建:
lhrpgdb=# select * from myschema.company;id | name | age | address | salary
----+------+-----+---------+--------
(0 rows)
刪除模式
刪除一個為空的模式(其中的所有對象已經(jīng)被刪除):
DROP SCHEMA myschema;
刪除一個模式以及其中包含的所有對象:
DROP SCHEMA myschema CASCADE;
查詢
select * from pg_catalog.pg_namespace;SELECT n.nspname AS "Name",pg_catalog.pg_get_userbyid(n.nspowner) AS "Owner",pg_catalog.array_to_string(n.nspacl, E'\n') AS "Access privileges",pg_catalog.obj_description(n.oid, 'pg_namespace') AS "Description"
FROM pg_catalog.pg_namespace n
ORDER BY 1;