天天看點

Mac安裝 PG13.3

1、通過brew安裝資料庫,預設安裝最新版本

brew list postgresql
brew install postgresql           

2、檢視下安裝的版本

rhl@rhldeMacBook-Pro log % pg_ctl -V      
pg_ctl (PostgreSQL) 13.3           

3、初始化資料庫

rhl@rhldeMacBook-Pro ~ % initdb -D /Users/rhl/other/postgresql -U rhl --lc-collate=C --lc-ctype=en_US.UTF-8 --lc-messages=en_US.UTF-8 -E UTF8 
The files belonging to this database system will be owned by user "rhl".
This user must also own the server process.

The database cluster will be initialized with locales
  COLLATE:  C
  CTYPE:    en_US.UTF-8
  MESSAGES: en_US.UTF-8
  MONETARY: zh_CN.UTF-8
  NUMERIC:  zh_CN.UTF-8
  TIME:     zh_CN.UTF-8
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /Users/rhl/other/postgresql ... 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 /Users/rhl/other/postgresql -l logfile start
               

4、修改配置檔案

vi postgresql.conf
listen_addresses = '0.0.0.0' 
port = 5432 
log_destination = 'csvlog'  
logging_collector = on  
log_directory = 'log'  
log_filename = 'postgresql-%a.log'  
log_truncate_on_rotation = on  
log_rotation_age = 1d  
log_rotation_size = 1GB           

5、啟動資料庫

pg_ctl -D /Users/rhl/other/postgresql  start

ps -ef|grep postgres           

6、建立user

rhl@rhldeMacBook-Pro var % psql
psql (13.3)
Type "help" for help.

rhl=# \l
                            List of databases
   Name    | Owner | Encoding | Collate |    Ctype    | Access privileges 
-----------+-------+----------+---------+-------------+-------------------
 postgres  | rhl   | UTF8     | C       | en_US.UTF-8 | 
 rhl       | rhl   | UTF8     | C       | en_US.UTF-8 | 
 template0 | rhl   | UTF8     | C       | en_US.UTF-8 | =c/rhl           +
           |       |          |         |             | rhl=CTc/rhl
 template1 | rhl   | UTF8     | C       | en_US.UTF-8 | =c/rhl           +
           |       |          |         |             | rhl=CTc/rhl
(4 rows)           

可以看到已存在使用者同名資料庫、postgres資料庫、template0、template1,但是postgres資料庫的所有者是目前使用者,沒有postgres使用者。是以我們需要建立postgres使用者。

CREATE USER postgres WITH PASSWORD 'postgres';
GRANT ALL PRIVILEGES ON DATABASE postgres to postgres;
ALTER ROLE postgres CREATEDB;
DROP DATABASE postgres;
CREATE DATABASE postgres OWNER postgres;

psql -U postgres -d postgres