想要用pg_ctl等一系列的命令,須要配置環境變量:web
PATH=$PATH:$HOME/.local/bin:$HOME/bin:/usr/local/pgsql/bin
export PGDATA=/usr/local/pgsql/data
export PATHsql
在.bash_profile 文件中添加上面的環境變量數據庫
而後source .bash_profile 使之生效。bash
在啓動的時候,報錯:pg_ctl: no database directory specified and environment variable PGDATA unsetsocket
須要把/usr/local/pgsql/data 的用戶名和屬組爲 postgres 而且目錄權限爲0700 post
chmod -R 0700 /usr/local/pgsql/dataspa
命令配置完成,能夠使用。操作系統
中止服務:命令行
[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
啓動服務:code
[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;