$ su - dbuser
Password:
Last login: Wed Mar 1 11:52:07 CST 2017 on pts/
[dbuser@master ~]$ psql -d exampledb
postgres=# \l
postgres=# CREATE DATABASE exampledb OWNER dbuser;
postgres=# GRANT ALL PRIVILEGES ON DATABASE exampledb TO dbuser;
postgres=# \q
$ sudo adduser dbuser
$ sudo passwd dbuser
\c dbname
\dt
\d tblname
\di
DROP DATABASE phone;
create table test(id integer not null primary key);
alter table phone add column phone_number character varying(11) not null;
alter table [表名A] rename to [表名B];
drop table [表名];
alter table [表名] add column [字段名] [类型];
alter table [表名] drop column [字段名];
alter table [表名] rename column [字段名A] to [字段名B];
alter table [表名] alter column [字段名] set default [新的默认值];
alter table [表名] alter column [字段名] drop default;
insert into 表名 ([字段名m],[字段名n],......) values ([列m的值],[列n的值],......);
//eg: insert into phone(id, phone_number) values(1, 18819259295);
update [表名] set [目标字段名]=[目标值] where [该行特征];
eg: update phone set phone_number = replace('phone_number','8', '1');
delete from [表名] where [该行特征];
delete from [表名];--删空整个表
create table ([字段名1] [类型1] ;,[字段名2] [类型2],......<,primary key (字段名m,字段名n,...)>;);
\copyright
\encoding [字元编码名称]
\h [名称]
\prompt [文本] 名称
\password [USERNAME] securely change the password for a user
pg_dump drupal>/opt/Postgresql/backup/1.bak
//eg: pg_dump test > /usr/local/pgsql/outputdata/db.sql
pg_dump:将一个PostgreSQL数据库转储到一个脚本文件或者其它归档文件中
pg_dump mydb > db.sql:将mydb数据库转储到一个 SQL 脚本文件
pg_dump mydb -s > db.sql:将mydb数据库转储到一个 SQL 脚本文件(只导出数据库表结构,不带数据)
pg_dump -D -a -t zones_seq - t interway -t table_3 ... > /tmp/zones_seq.sql:导出部分表,支持通配符,见参考文章1
pg_dumpall:将一个PostgreSQL数据库集群转储到一个脚本文件中
示例:test为待导出的数据库名,后面为导出位置及文件名称
pg_dump test > /usr/local/pgsql/outputdata/db.sql
psql:psql -U gpadmin -d your-db -f your-table.sql
pg_restore: pg_restore -d your-db your-table.tar
示例:postgresql为用户名,test为要导入的数据库名称,后面为先前导出的数据库文件路径
psql -U postgresql -d test -f /usr/local/pgsql/outputdata/db.sql
select * from phone;
~> su - postgres
Password:
Last login: Wed Mar 1 13:19:02 CST 2017 on pts/1
-bash-4.2$ psql
psql (9.2.18)
Type "help" for help.
postgres=# create user xxf with password '******';
CREATE ROLE
postgres=# create database xxf owner xxf;
CREATE DATABASE
postgres=# grant all privileges on database xxf to xxf;
GRANT
postgres=# \q
-bash-4.2$ exit
logout
~> psql
psql (9.2.18)
Type "help" for help.
xxf=>
至此,就在数据库xxf中了。