1. 程式人生 > >mac電腦os x EI Capitan系統下解壓版postgreSQL使用

mac電腦os x EI Capitan系統下解壓版postgreSQL使用

       最近沒事研究postgreSQL和學習python3,於是在自己的mac電腦上試驗了一下postgreSQL的使用。由於不想用安裝版的postgreSQL版本,所以直接使用瞭解壓版的postgreSQL版本(下載地址https://www.enterprisedb.com/download-postgresql-binaries )。下載到zip包後直接解壓,目錄結構如下:


這裡也就懶得設定什麼環境變量了,直接開始幹吧。

先進入bin目錄下,執行initdb檔案,如下:

YZQ-MacBook-Pro:bin yinzeqiang$ ./initdb  -D /Users/yinzeqiang/softDev/pgsql/dataFiles/ -E utf8
The files belonging to this database system will be owned by user "yinzeqiang".
This user must also own the server process.

The database cluster will be initialized with locale "zh_CN.UTF-8".
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.

fixing permissions on existing directory /Users/yinzeqiang/softDev/pgsql/dataFiles ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
creating template1 database in /Users/yinzeqiang/softDev/pgsql/dataFiles/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
syncing data to disk ... ok

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:

1.initdb命令

./initdb  -D /Users/yinzeqiang/softDev/pgsql/dataFiles/ -E utf8  命令,我的理解就是搞一個數據檔案儲存目錄初始化資料庫吧。-D後跟資料庫資料檔案儲存路徑,-E後跟編碼。

2.pg_ct命令

pg_ct命令可以開始資料庫服務或者關閉資料庫。

./pg_ctl -D /Users/yinzeqiang/softDev/pgsql/dataFiles/ -l /Users/yinzeqiang/softDev/pgsql/dataFiles/postgrepsql.log  start命令,開始資料庫服務。-D後跟資料庫資料檔案儲存

徑,-l後跟日誌檔案地址。

./pg_ctl stop -w -D /Users/yinzeqiang/softDev/pgsql/dataFiles/ -m smart命令結束資料庫服務。-D後跟資料庫資料檔案儲存路徑.-m後跟資料庫停止或者重啟的模式:

一共有三種模式:

  smart       quit after all clients have disconnected

  fast        quit directly, with proper shutdown

  immediate   quit without complete shutdown; will lead to recovery on restart

3.createdb和createuser命令

YZQ-MacBook-Pro:bin yinzeqiang$ ./createuser -a -d -e -P python3learner
Enter password for new role: 
Enter it again: 
CREATE ROLE python3learner PASSWORD 'md5fb444479c9daa2b26fa4d6c045699070' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN;

createuser命令建立使用者,用法:createuser [OPTION]... [ROLENAME]

OPTION可用引數如下:

    [-a]:允許建立其他使用者,相當於建立一個超級使用者;
    [-A]:不允許此使用者建立其他使用者;
    [-d]:允許此使用者建立資料庫;
    [-D]:不允許此使用者建立資料庫;
    [-e]:將執行過程顯示到Shell上;
    [-P]:建立使用者時,同時設定密碼;
    [-h 主機名]:為某個主機上的Postgres建立使用者;
    [-p port]:與-h引數一同使用,指定主機的埠。

createdb命令用於建立資料庫,用法:createdb [OPTION]... [DBNAME] [DESCRIPTION]。如下:

./createdb -E utf8 testdb  建立名為testdb的資料庫,-E後跟資料庫編碼

4.psql命令

psql可以連線資料庫,如下:

YZQ-MacBook-Pro:dataFiles yinzeqiang$ psql -d testdb
psql (9.5.5)
Type "help" for help.

testdb=# \d
                           List of relations
 Schema |               Name                |   Type   |     Owner      
--------+-----------------------------------+----------+----------------
 public | auth_group                        | table    | python3learner
 public | auth_group_id_seq                 | sequence | python3learner
 public | auth_group_permissions            | table    | python3learner
 public | auth_group_permissions_id_seq     | sequence | python3learner
 public | auth_permission                   | table    | python3learner
psql -d testdb連線到testdb資料庫。

5.所有命令可以通過 '命令 --help'的形式檢視用法,稍微懂一點英語都可以看懂

(IT菜鳥,有錯誤請指正)