--创建用户 CREATE ROLE pguser WITH ENCRYPTED PASSWORD 'pguser'; --创建表空间目录 mkdir -p /database/pg10/pg_tbs/tbs_mydb --创建表空间 CREATE TABLESPACE tbs_mydb OWNER pguser LOCATION '/database/pg10/pg_tbs/tbs_mydb'; --创建数据库 CREATE DATABSE mydb WITH OWNER = pguser TEMPLATE=template0 ENCODING='UTF8' TABLESPACE=tbs_mydb; --赋权 GRANT ALL ON DATABASE mydb TO pguser WITH GRANT OPTION; GRANT ALL ON TABLESPACE tbs_mydb TO pguser;
1. psql 元命令介绍
1.1 \l 列出所有数据库列表
postgres=# \l List of databases Name | Owner | Encoding | Collation | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- ambari | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres : postgres=CTc/postgres : ambari=CTc/postgres ambarirca | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres : postgres=CTc/postgres : mapred=CTc/postgres mydb | 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 (6 rows)
1.2 、\db 查看表空间列表
postgres=# \db List of tablespaces Name | Owner | Location ------------+----------+---------- pg_default | postgres | pg_global | postgres | (2 rows)
1.3 \d查看表定义
postgres=# \dt List of relations Schema | Name | Type | Owner --------+--------------------------+-------+---------- public | cloud_sql_static | table | postgres public | cloud_subscriber_devices | table | postgres public | cloud_subscribers | table | postgres public | cloud_table_static | table | postgres public | sxacc-device-types | table | postgres public | sxacc-devices | table | postgres (6 rows) postgres=# \d cloud_table_static Table "public.cloud_table_static" Column | Type | Modifiers ---------------------+--------------------------+----------- relname | character varying(255) | seq_scan | bigint | seq_tup_read | bigint | idx_scan | bigint | idx_tup_fetch | bigint | n_tup_ins | bigint | n_tup_upd | bigint | n_tup_del | bigint | n_tup_hot_upd | bigint | n_live_tup | bigint | n_dead_tup | bigint | n_mod_since_analyze | bigint | last_vacuum | timestamp with time zone | last_autovacuum | timestamp with time zone | last_analyze | timestamp with time zone | last_autoanalyze | timestamp with time zone | vacuum_count | bigint | autovacuum_count | bigint | analyze_count | bigint | autoanalyze_count | bigint | import_date | timestamp with time zone | postgres=#
1.4 查看表/索引占用空间大小
给测试表test插入500万数据:
postgres=# create table test(id int primary key, name varchar(100)); CREATE TABLE postgres=# insert into test(id,name) select n,n||'_francs' from generate_series(1,5000000) n; INSERT 0 5000000 postgres=# \di+ test_pkey List of relations Schema | Name | Type | Owner | Table | Size | Description --------+-----------+-------+----------+-------+--------+------------- public | test_pkey | index | postgres | test | 107 MB | (1 row) postgres=# \dt+ test List of relations Schema | Name | Type | Owner | Size | Description --------+------+-------+----------+--------+------------- public | test | table | postgres | 249 MB | (1 row)
1.5 \sf 查看函数代码
mydb#\sf random_range CREATE OR REPLACE FUNCTION pguser.random_range(integer,integer) RETURN integer LANGUAGE sql AS $function$ SELECT ($1 + FLOOR(($2-$1 +1 )* random()))::int4; $function$
上述\sf命令后面可以只接函数的名称,或者函数名称及输入参数类型,例如random_range(integer,integer),Postgres支持名称相同但输入参数类型不同的函数,如果有同名函数,\sf 必须指定参数类型。
1.6 \x 设置查询结果输出
postgres=# \x Expanded display is on. postgres=# select * from test limit 1; -[ RECORD 1 ]-- id | 1 name | 1_francs
1.7 -E 获取元命令对应的SQL代码
postgres:~$ psql -E psql (9.5.14) Type "help" for help. postgres=# \db ********* QUERY ********** SELECT spcname AS "Name", pg_catalog.pg_get_userbyid(spcowner) AS "Owner", pg_catalog.pg_tablespace_location(oid) AS "Location" FROM pg_catalog.pg_tablespace ORDER BY 1; ************************** List of tablespaces Name | Owner | Location ------------+----------+---------- pg_default | postgres | pg_global | postgres | (2 rows) postgres=#