首页 > 代码库 > Working with Data Source 12

Working with Data Source 12

In command line, psql helps you to manage and connect the database:1

1. Type psql in command line to enter the psql command line tool.

2. In the shell, type \q to exit.

3. In the psql tool, we can create a database by directly typing:

   CREATE DATABASE bank_accounts;

4. To enter the database, we can use:

   psql database_name 

5. In the database, we can create a table:

  create table deposits(id integer primary key, name text, amount float); # Do not forget semicolon in the end.

6.  Create role, login, create password and role authorizaion:

  CREATE ROLE userName WITH CREATEDB LOGIN PASSWORD `password`;# Create role: create a user; CREATEDB: the owner has authorization of creating database; login: automatically login with current username; password: with the passwork

7. Give user privileges:

  GRANT SELECT, INSERT, UPDATE, DELETE ON tableName TO userName; # Allow user to select,insert,update,delete on the table

  GRANT ALL PRIVILEGES ON tableName TO userName;# allow people to do anything on the table

8. Remove permissions:

  REVOKE SELECT, INSERT, UPDATE, DELETE ON tableName FROM userName;

  REVOKE ALL PRIVILEGES ON tableName FROM userName;

9.  \l -- list all available databases.

   \dt -- list all tables in the current database.  

   \du -- list users.

   

Working with Data Source 12