1. 修改PostgreSQL數(shù)據(jù)庫默認用戶postgres的密碼
[root@izwz90tx4egvh4qj3p95vsz ~]# sudo -u postgres psql
psql (10.4)
Type "help" for help.
postgres=# ALTER USER postgres WITH PASSWORD 'duyeweb';
ALTER ROLE
注意:
- 密碼postgres要用引號引起來
- 命令最后有分號
2. 修改linux系統(tǒng)postgres用戶的密碼
步驟一:刪除用戶postgres的密碼
[root@izwz90tx4egvh4qj3p95vsz ~]# sudo passwd -d postgres
Removing password for user postgres.
passwd: Success
步驟二:設置用戶postgres的密碼
[root@izwz90tx4egvh4qj3p95vsz ~]# sudo -u postgres passwd
Changing password for user postgres.
New password:
BAD PASSWORD: The password fails the dictionary check - it does not contain enough DIFFERENT characters
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
3. 使用超級用戶postgres創(chuàng)建新數(shù)據(jù)庫和用戶
- 創(chuàng)建數(shù)據(jù)庫duyeweb
[root@izwz90tx4egvh4qj3p95vsz ~]# sudo -u postgres psql
could not change directory to "/root": Permission denied
psql (10.4)
Type "help" for help.
postgres=# create database duyeweb;
CREATE DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
duyeweb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres+
| | | | | duye=C/postgres
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
- 創(chuàng)建新用戶duye
- 設置密碼'123456'
- 賦予登錄和創(chuàng)建數(shù)據(jù)庫對象的權限
[root@izwz90tx4egvh4qj3p95vsz ~]# sudo -u postgres psql
could not change directory to "/root": Permission denied
psql (10.4)
Type "help" for help.
postgres=# create user duye;
CREATE ROLE
postgres=# alter user duye password '123456';
ALTER ROLE
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
duye | | {}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
postgres=# alter user duye createrole createdb replication login;
ALTER ROLE
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
duye | Create role, Create DB, Replication | {}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
- 修改客戶端認證方式:vim /var/lib/pgsql/10/data/pg_hba.conf
更多說明:http://www.reibang.com/p/b2dbc3a0402d
修改用戶的認證方式為md5:
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 ident
host replication all ::1/128 ident
- 重新加載配置:
systemctl reload postgresql-10
- 使用新用戶duye登錄新的數(shù)據(jù)庫duyeweb
[root@izwz90tx4egvh4qj3p95vsz ~]# psql duyeweb -Uduye -W
Password for user duye:
psql (10.4)
Type "help" for help.
duyeweb=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
duyeweb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres+
| | | | | duye=C/postgres
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
(4 rows)
duyeweb=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
duye | Superuser, Create role, Create DB, Replication | {}
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
duyeweb=#
4. 創(chuàng)建模式
schema是邏輯別構(gòu)格了,將數(shù)據(jù)庫進行邏輯劃分。同一數(shù)據(jù)庫下可以有多個schema徽鼎,不同數(shù)據(jù)庫下的schema互不相關盛末。
查看模式
duyeweb=# select current_schema;
current_schema
----------------
public
(1 row)
duyeweb=# show search_path
search_path
-----------------
"$user", public
(1 row)
duyeweb=# \dn
List of schemas
Name | Owner
--------+----------
public | postgres
(1 row)
duyeweb=# select * from pg_catalog.pg_namespace order by 1;
nspname | nspowner | nspacl
--------------------+----------+-------------------------------------
information_schema | 10 | {postgres=UC/postgres,=U/postgres}
pg_catalog | 10 | {postgres=UC/postgres,=U/postgres}
pg_temp_1 | 10 |
pg_toast | 10 |
pg_toast_temp_1 | 10 |
public | 10 | {postgres=UC/postgres,=UC/postgres}
(6 rows)
創(chuàng)建一個模式
duyeweb=# \h create schema
Command: CREATE SCHEMA
Description: define a new schema
Syntax:
CREATE SCHEMA schema_name [ AUTHORIZATION role_specification ] [ schema_element [ ... ] ]
CREATE SCHEMA AUTHORIZATION role_specification [ schema_element [ ... ] ]
CREATE SCHEMA IF NOT EXISTS schema_name [ AUTHORIZATION role_specification ]
CREATE SCHEMA IF NOT EXISTS AUTHORIZATION role_specification
where role_specification can be:
user_name
| CURRENT_USER
| SESSION_USER
duyeweb=# CREATE SCHEMA IF NOT EXISTS duyeweb;
CREATE SCHEMA
duyeweb=# \dn
List of schemas
Name | Owner
---------+----------
duyeweb | duye
public | postgres
(2 rows)
發(fā)現(xiàn)更多寶藏
我在喜馬拉雅上分享聲音
《PostgreSQL數(shù)據(jù)庫內(nèi)核分析》,點開鏈接可以聽聽否淤,有點意思悄但。
《數(shù)據(jù)庫系統(tǒng)概論(第4版)》,點開鏈接可以聽聽石抡,有點意思檐嚣。
其他相關文章分享列表:
第 23 課 PostgreSQL 創(chuàng)建自己的數(shù)據(jù)庫啰扛、模式嚎京、用戶
第 22 課 PostgreSQL 控制文件
第 21 課 PostgreSQL 日志系統(tǒng)
第 16 課 查詢過程源碼分析
第 15 課 PostgreSQL 系統(tǒng)參數(shù)配置
第 14 課 PostgreSQL 數(shù)據(jù)存儲結(jié)構(gòu)
第 13 課 PostgreSQL 存儲之Page(頁面)源碼分析
第 12 課 PostgreSQL 認證方式
第 11 課 PostgreSQL 增加一個內(nèi)核C函數(shù)
第 10 課 PostgreSQL 在內(nèi)核增加一個配置參數(shù)
第 09 課 PostgreSQL 4種進程啟動方式
第 08 課 PostgreSQL 事務介紹
第 07 課 PostgreSQL 數(shù)據(jù)庫、模式隐解、表鞍帝、空間、用戶間的關系
第 06 課 PostgreSQL 系統(tǒng)表介紹
第 05 課 PostgreSQL 編譯源代碼進行開發(fā)
第 04 課 PostgreSQL 安裝最新的版本
第 03 課 PostgreSQL 代碼結(jié)構(gòu)
第 02 課 PostgreSQL 的特性煞茫、應用帕涌、安裝
第 01 課 PostgreSQL 簡介及發(fā)展歷程
上面文章都在專輯中:PostgreSQL專輯鏈接摄凡,點我查看
如果有用,可以收藏這篇文件蚓曼,隨時在更新....
更多交流加群: PostgreSQL內(nèi)核開發(fā)群 876673220
親亲澡,記得點贊、留言辟躏、打賞額9瓤邸!捎琐!