summary
Common commands for PostgreSQL
1. Log in to the database
/* Switch to database user */ su - postgres /* Log in */ psql
The login is displayed as follows:
bash-4.2$ psql psql (9.3.17) Type "help" for help. postgres=>
2. Switch the database
/* Log in to the specified database */ psql -U user -d dbname /* List database */ \l /* Switch database */ \c dbname
3. User Management
/* Create user */ CREATE ROLE rolename; CREATE USER username WITH PASSWORD '*****'; /* Show all users */ \du /* Modify user permissions */ ALTER ROLE username WITH privileges; /* All permissions assigned to the user table */ GRANT ALL ON tablename TO user; /* All permissions assigned to the user database */ GRANT ALL PRIVILEGES ON DATABASE dbname TO dbuser; /* Revoke user permissions */ REVOKE privileges ON tablename FROM user; /* Revoke user permissions */
4. Database operations
/* Create a database */ create database dbname; /* Delete the database */ drop database dbname;
5. Table operation
/* Add permission to increase the primary key itself */ grant all on sequence tablename_keyname_seq to webuser; /* Rename a table */ alter table [Table nameA] rename to [Table nameB]; /* Delete a table */ drop table [Table name]; /* Add fields in existing tables */ alter table [Table name] add column [Field name] [type]; /* Delete fields in the table */ alter table [Table name] drop column [Field name]; /* Rename a field */ alter table [Table name] rename column [Field nameA] to [Field nameB]; /* Set default value for a field */ alter table [Table name] alter column [Field name] set default [New default value]; /* Remove default value */ alter table [Table name] alter column [Field name] drop default; /* Insert data */ insert into Table name ([Field namem],[Field namen],......) values ([ListmValue of],[ListnValue of],......); /* Modify data */ update [Table name] set [目标Field name]=[Target value] where ...; /* Delete data */ delete from [Table name] where ...; /* Delete table */ delete from [Table name]; /* Query */ SELECT * FROM dbname WHERE ...; /* Create table */ create table ( [Field name1] [type1] primary key, [Field name2] [type2], ......, [Field namen] [Field namen] )
6. Exit
\q quit
Supplement: postgresql authorizes the permissions of a database to the test account so that the account can only operate the specified DB and cannot operate other DBs.
alter user test set default_transaction_read_only=on; grant all on database crm_db to test; grant select on all tables in schema public to test; // What works is this sentence: To enter the crm_db operation, the db permission will be granted in which db environment
Undo before deletion
revoke all on database crm_prod_myl from test; revoke select on all tables in schema public from test;
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.