【postgres】創建用戶與數據庫


1. 用戶及數據庫創建

sudo -u postgres psql

# 創建用戶
postgres
=# create user test with password '123456'; # 創建數據庫並指定擁有者
postgres
=# create database testdb owner test; # 給指定數據庫的所有權限綁定用戶
postgres
=# grant all privileges on database testdb to test; # 查看用戶
postgres
=# \du # 查看數據庫
postgres
=# \l

2. 創建過程

tester@fabu:~$ sudo -u postgres psql
psql (14.1 (Ubuntu 14.1-1.pgdg18.04+1))
Type "help" for help.

postgres=# create user test with password '123456';
CREATE ROLE
postgres=# create database testdb owner test;
CREATE DATABASE
postgres=# grant all privileges on database testdb to test;
GRANT
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 test      |                                                            | {}

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 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
 testdb    | test     | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/test             +
           |          |          |             |             | test=CTc/test

 3. 刪除用戶和數據庫

postgres=# DROP DATABASE IF EXISTS testdb;
DROP DATABASE
postgres=# DROP USER test;
DROP ROLE
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 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
(3 rows)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM