想要用pg_ctl等一系列的命令,需要配置環境變量:
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/local/pgsql/bin
export PGDATA=/usr/local/pgsql/data
export PATH
在.bash_profile 文件中添加上面的環境變量
然后source .bash_profile 使之生效。
在啟動的時候,報錯:pg_ctl: no database directory specified and environment variable PGDATA unset
需要把/usr/local/pgsql/data 的用戶名和屬組為 postgres 並且目錄權限為0700
chmod -R 0700 /usr/local/pgsql/data
命令配置完成,可以使用。
停止服務:
[postgres@master pgsql]$ pg_ctl stop waiting for server to shut down...2018-12-20 17:27:15.726 CST [30069] LOG: received fast shutdown request .2018-12-20 17:27:15.728 CST [30069] LOG: aborting any active transactions 2018-12-20 17:27:15.728 CST [30214] FATAL: terminating connection due to administrator command 2018-12-20 17:27:15.729 CST [30213] FATAL: terminating connection due to administrator command 2018-12-20 17:27:15.730 CST [30215] FATAL: terminating connection due to administrator command 2018-12-20 17:27:15.732 CST [30069] LOG: worker process: logical replication launcher (PID 30076) exited with exit code 1 2018-12-20 17:27:15.732 CST [30216] FATAL: terminating connection due to administrator command 2018-12-20 17:27:15.735 CST [30071] LOG: shutting down 2018-12-20 17:27:15.745 CST [30069] LOG: database system is shut down done server stopped
啟動服務:
[postgres@master pgsql]$ pg_ctl start waiting for server to start....2018-12-20 17:27:53.811 CST [30324] LOG: listening on IPv4 address "0.0.0.0", port 5432 2018-12-20 17:27:53.811 CST [30324] LOG: listening on IPv6 address "::", port 5432 2018-12-20 17:27:53.813 CST [30324] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432" 2018-12-20 17:27:53.828 CST [30325] LOG: database system was shut down at 2018-12-20 17:27:15 CST 2018-12-20 17:27:53.830 CST [30324] LOG: database system is ready to accept connections done server started
登入數據庫默認用戶為postgres
[postgres@master pgsql]$ psql psql (10.5) Type "help" for help. postgres=# select user; user ---------- postgres (1 row) postgres=#
指定用戶登入-zhang
[postgres@master pgsql]$ psql -d mydb -U zhang psql (10.5) Type "help" for help. mydb=>
默認用戶
postgres安裝完成后,會自動在操作系統和postgres數據庫中分別創建一個名為postgres的用戶以及一個同樣名為postgres的數據庫。
psql -U zhang -d mydb -h 192.168.1.200 -W
[postgres@master ~]$ psql -U zhang -d mydb -h 192.168.1.200 -W Password for user zhang: psql (10.5) Type "help" for help. mydb=>
參數含義: -U
指定用戶 -d
要連接的數據庫 -h
要連接的主機 -W
提示輸入密碼。
創建用戶:
createuser username 這個用法和下面的create user 是一樣的
在PostgresSQL命令行中使用CREATE ROLE
指令創建:
CREATE ROLE rolename;
在PostgresSQL命令行中使用CREATE USER
指令創建
CREATE USER username;
CREATE USER
和CREATE ROLE
的區別在於,CREATE USER
指令創建的用戶默認是有登錄權限的,而CREATE ROLE
沒有。
創建用戶時設定用戶屬性
CREATE ROLE role_name WITH optional_permissions;
創建用戶時設定登錄權限。
CREATE ROLE username WITH LOGIN;
修改用戶屬性
ALTER ROLE username WITH attribute_options;
通過以下方式禁止用戶登錄
ALTERROLE username WITH NOLOGIN;
設置訪問權限
GRANT permission_type ON table_name TO role_name;
eg: GRANT UPDATE ON tablename TO use_role; --賦予use_role tablename表的update權限 GRANT SELECT ON ALL TABLES IN SCHEMA PUBLIC to use_role; --賦予use_role所有表的SELECT權限
特殊符號:ALL
代表所訪問權限,PUBLIC
代表所有用戶
GRANT ALL ON demo TO demo_role; --賦給用戶所有權限 GRANT SELECT ON demo TO PUBLIC; --將SELECT權限賦給所有用戶
撤銷用戶訪問權限
語法格式如下:
REVOKE permission_type ON table_name FROM user_name;