[Complete list of commonly used commands in Postgresql] - Common commands in Pgsql

Common commands for Postgresql database

1. Connect to the database. The default user and database are postgres.

psql -h host -p port -U user -d dbname

2. Execute sql file

psql -h ${PGHOST} -p ${PGPORT} -U ${PGUSER} -d safe_browser -f xxxx.sql

\i  /xxxxx/xxx/xxxxxx.sql

3. Switch database, equivalent to use dbname of mysql

\c dbname

4. List databases, equivalent to mysql’s show databases

\l

5. List tables, equivalent to mysql’s show tables

\dt

6. View the table structure, equivalent to desc tblname, show columns from tbname

\d tblname

7. \di View index

8. Create database:

create database [数据库名];

createdb -h ${PGHOST} -p ${PGPORT} -U ${PGUSER} dbname

9. Delete database

drop database [数据库名];

10. Rename a table

alter table [表名A] rename to [表名B];

11. Delete a table

drop table [表名];

12. Add fields to existing tables

alter table [表名] add column [字段名] [类型];

13. Delete fields in the table

alter table [表名] drop column [字段名];

14. Rename a field

alter table [表名] rename column [字段名A] to [字段名B];

15. Set a default value for a field

alter table [表名] alter column [字段名] set default [新的默认值];

16. Remove the default value

alter table [表名] alter column [字段名] drop default;

17. Insert data into the table

insert into 表名 ([字段名m],[字段名n],…) values ([列m的值],[列n的值],…);

18. Modify the data of a certain row and column in the table

update [表名] set [目标字段名]=[目标值] where [该行特征];

19. Delete a row of data in the table

delete from [表名] where [该行特征];
delete from [表名];–删空整个表

20. Create table

create table ([字段名1] [类型1] <references 关联表名(关联的字段名)>;,[字段名2] [类型2],…<,primary key (字段名m,字段名n,…)>;);

21. Export the entire library

su - postgres
pg_dump -h hlpgsqlykf -p 5876  -U hlpostgres  activiti>activiti.sql  (注意>符号前后不能有空格)

22. Export a table (-t)

pg_dump -h hlpgsqlykf -p 5876 -U hlpostgres activiti -t tablename>activiti.sql (note that there cannot be spaces before and after the > symbol)

23. Export only the table structure (-s)

pg_dump -h hlpgsqlykf -p 5876  -U hlpostgres -s activiti>activiti.sql  (注意>符号前后不能有空格)

24. Target server import

su - postgres
createdb -h 172.28.17.221 -p 5876 -U hlpostgres activiti (创建数据库)
psql -h 172.28.17.221 -p 5876 -U hlpostgres -d activiti -f activiti.sql 

25. Display PostgreSQL terms of use and distribution

\copyright 

26. Display the character encoding name or set the client character encoding

\encoding

27. Set user password

\password [USERNAME]

28. Exit psql

\q 

Guess you like

Origin blog.csdn.net/YJ000312/article/details/132231377