使用终端安装 PostgreSQL 和 createdb

Installing PostgreSQL and `createdb` with Terminal

使用 Homebrewterminal 安装 PostgreSQL 后...

➜ ~ brew link postgresql

Warning: Already linked: /usr/local/Cellar/postgresql/11.2_1: 3,186 files, 35.3MB

To relink: brew unlink postgresql && brew link postgresql

➜ ~ brew 服务重启 postgresql

Successfully stopped postgresql (label: homebrew.mxcl.postgresql)

Successfully started postgresql (label: homebrew.mxcl.postgresql)

➜ ~ createdb 'test'

createdb: could not connect to database template1: could not connect to server: No server file or directory

Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

我希望能够 运行 严格地从终端,而不是使用 PSequel GUI...

谢谢,

已解决

我的主要问题是:

➜ ~ createdb 'test'

createdb: could not connect to database template1: could not connect to server: No server file or directory  
Is the server running locally and accepting connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

这个问题简单地解决了:

➜ ~ postgres -D /usr/local/var/postgres

调用了数据库目录...

2019-05-06 14:20:37.367 EDT [41854] LOG:  listening on IPv6 address "::1", port 5432  
2019-05-06 14:20:37.367 EDT [41854] LOG:  listening on IPv4 address "127.0.0.1", port 5432  
2019-05-06 14:20:37.367 EDT [41854] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"  
2019-05-06 14:20:37.384 EDT [41854] LOG:  database system is ready to accept connections

保持 运行,并在我的终端上打开一个新标签:

➜ ~ createdb 'test'

➜ ~ psql 'test'

psql (11.2)

Type "help" for help.

test=#

我在调试期间使用的其他东西:

删除旧的数据库文件(这很危险)

➜ ~ rm -rf /usr/local/var/postgres

跟进

➜ ~ initdb /usr/local/var/postgres

initdb不用于创建“新数据库”

As Documented in the Manual 您需要它来创建一个“集群”或“数据目录”,然后存储使用 createdb

创建的数据库

引自手册:

Before you can do anything, you must initialize a database storage area on disk. We call this a database cluster. (The SQL standard uses the term catalog cluster.) A database cluster is a collection of databases that is managed by a single instance of a running database server

[...]

In file system terms, a database cluster is a single directory under which all data will be stored. We call this the data directory or data area

简而言之:initdb 在硬盘上创建必要的目录布局,以便能够创建和管理数据库。

它是 Postgres 服务器安装过程的必要部分。