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