Postgresql初始化用戶
CREATE USER 用戶名 WITH PASSWORD '123456';GRANT ALL PRIVILEGES ON DATABASE 數據庫實例 TO 用戶名;GRANT ALL PRIVILEGES ON all tables in schema public TO 用戶名;
pgadmin4登錄給用戶授權賬戶 數據庫再切換用戶
Postgresql實例開啟和關閉
stop -m smart/fast/immidiate
smart等待所有連接關閉之后才關閉
fast快速關閉數據庫,斷開客戶端的連接,讓已有的事務回滾,然后正常關閉數據庫
immidiate:立即關閉數據庫,相當于數據庫進行立即停止,直接退出
stop -o SIGTERM/SIGINT/SIGQUIT/SIGKILL
SIGTERM 不再允許新的連接,但是允許所有活躍的會話正常完成他們的工作,只有在所有會話都結束任務后才關閉。這是智能關閉。
SIGINT 不再允許新的連接,向所有活躍服務器發送 SIGTERM(讓它們立刻退出),然后等待所有子進程退出并關閉數據庫。這是快速關閉。
SIGQUIT 令 postgres 向所有子進程發送 SIGQUIT 并且立即退出(所有子進程也會立即退出),而不會妥善地關閉數據庫系統。這是立即關閉。這樣做會導致下次啟動時的恢復(通過重放 WAL 日志)。我們推薦只在緊急的時候使用這個方法。
SIGKILL 此選項盡量不要使用,這樣會阻止服務器清理共享內存和信號燈資源,那樣的話你只能在啟動服務器之前自己手工做這件事。另外,SIGKILL 直接把 postgres 殺掉,而不會等它把信號中繼給它的子進程,因此我們還需要手工殺掉每個獨立子進程。
今年個人搭建項目需要使用PostgreSQL12.2數據庫,在這里分享下安裝過程,有需要的童鞋可以參考下。
PostgreSQL是自由的對象-關系型數據庫服務器,在靈活的BSD風格許可證下發行。
更多知識,可以搜索:
下面將開始介紹從下載、安裝到使用的說明。
下載頁面:https://www.postgresql.org/ftp/source/
$ wget https://ftp.postgresql.org/pub/source/v12.2/postgresql-12.2.tar.gz
# yum install readline-devel
# yum install zlib-devel
$ tar zxf postgresql-12.2.tar.gz
$ cd postgresql-12.2
$ mkdir -p ~/3rd/postgresql-12.2
$ ./configure --prefix=/home/testerzhang/3rd/postgresql-12.2
$ make
$ make install
安裝成功,OK
$ vim ~/.bash_profile
export PGHOME=$HOME/3rd/postgresql-12.2
export PGDATA=$PGHOME/data
export PATH=$PGHOME/bin:$PATH
$ source ~/.bash_profile
$ which psql
~/3rd/postgresql-12.2/bin/psql
$ which initdb
~/3rd/postgresql-12.2/bin/initdb
$ initdb
initdb: error: no data directory specified
You must identify the directory where the data for this database system
will reside. Do this with either the invocation option -D or the
environment variable PGDATA.
為啥會報錯?
如果沒有配置環境變量PGDATA,就會報上面這個錯,也可以指定[-D, --pgdata=]DATADIR location for this database cluster解決問題。
我們上面已經配置,直接執行就可以了。
$ initdb
The files belonging to this database system will be owned by user "testerzhang".
This user must also own the server process.
The database cluster will be initialized with locale "zh_CN.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"
The default text search configuration will be set to "simple".
Data page checksums are disabled.
creating directory /home/testerzhang/3rd/postgresql-12.2/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Shanghai
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
pg_ctl -D /home/testerzhang/3rd/postgresql-12.2/data -l logfile start
看到上面信息代表成功了。
一般配置文件目錄在data目錄,具體看下面例子:
$ pwd
/home/testerzhang/3rd/postgresql-12.2
$ find . -name pg_hba.conf
./data/pg_hba.conf
下面開始編輯配置文件
$ vim ./data/pg_hba.conf
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
#host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
這里注釋原來的記錄加入了:修改為0.0.0.0/0,加密方式改為md5,就表示需要密碼訪問
host all all 0.0.0.0/0 md5
listen_addresses修改為監聽整個網絡,請根據實際修改。
$ vim ./data/postgresql.conf
#listen_addresses='localhost' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
listen_addresses='*'
-l表示日志文件目錄,通常需要指定,所以我們在/usr/local/postgresql根目錄下再創建一個log目錄用來存放日志文件(注意別忘記賦予可寫的權限)。
$ pg_ctl -D /home/testerzhang/3rd/postgresql-12.2/data -l logfile start
創建當前系統普通用戶的數據庫:
$ createdb testerzhang
連接數據庫,默認連接跟系統用戶名一樣的數據庫。
$ psql
psql (12.2)
Type "help" for help.
testerzhang=#
testerzhang-# \q
如果需要指定連接到某個數據庫(test_db),可以用psql -d test_db 。
testerzhang=# select * from pg_user;
usename | usesysid | usecreatedb | usesuper | userepl | usebypassrls | passwd | valuntil | useconfig
---------+----------+-------------+----------+---------+--------------+----------+----------+-----------
testerzhang | 10 | t | t | t | t | ******** | |
(1 row)
修改密碼
testerzhang=# alter user testerzhang password 'qwerty';
ALTER ROLE
$ pg_ctl stop
\l
\c database
\c username
\dt
\d table
好了,今天就分享到這里了,你Get到了嗎?
我是testerzhang,喜歡本文的童鞋,可以關注我+收藏,不明白的地方也可以評論留言。