刚入手MAC一周,算是从fedora 20上迁移过来的吧,感觉还是蛮适应的。
先装上mac得包管理工具brew,官网为Homebrew,官网有详细的教程,不过就是一些常用的安装( install ),查询( search )等一些常用的操作。
说正事,先安装postgresql:
brew install postgresql
查看已安装的pg版本:
pg_ctl -V
安装成功之后,安装路径为:
/usr/local/var/postgres
初始化数据库:
initdb /usr/local/var/postgres这里要特别注意的是,搜索到的很多方法都是:initdb /usr/local/var/postgres -E utf8但是这样初始化可以,但是最后启动pg的时候总是启动不起来,失败了无数次之后才知道真不能这么干,具体原因还不清楚。
手动启动数据库:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
查看数据库状态:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log status停止:pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log stop -s -m fast
查看数据库进程:ps -ef |grep postgres 或 ps auxwww | grep postgres创建一个数据库用户:createuser username -P创建数据库:createdb dbname -O username -E UTF8 -e这样就建好了,本地就可以用pgAdmin访问了。
从其他数据库导入导出表结构和数据以后再说。
远程导出表结构和数据:
pg_dump -h 104.236.131.89 hold -U hold -p 5432 -f hold.dump远程导出表结构,不带数据,因为数据量一般都很大,导出超级慢,所有可以只导出表结构: pg_dump -s -h 104.236.131.89 hold -U hold -p 5432 -f hold.dump导入本地数据库:psql -h 0.0.0.0 hold -U hold -p 5432 -f hold.dump
特别注意:如果你用上述的命令启动失败,就去 /usr/local/var/postgres/server.log 查看错误日志,比如我启动报错:
LOG: could not translate host name "localhost", service "5432" to address: nodename nor servname provided, or not known
WARNING: could not create listen socket for "localhost"
FATAL: could not create any TCP/IP sockets
看错误是localhost的问题,无法翻译主机名"localhost",到配置文件 /usr/local/var/postgres/postgresql.conf 里修改一下配置文件:
listen_addresses = '0.0.0.0' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
#port = 5432 # (change requires restart)把listen_addresses由"localhost"改为"0.0.0.0"即可。
重新用启动命令启动一下数据库就OK。
本文介绍了在Mac上使用Homebrew安装PostgreSQL的过程,包括查看已安装版本、初始化数据库、启动数据库和查看数据库状态。当遇到启动问题时,通过检查错误日志并修改配置文件来解决。

被折叠的 条评论
为什么被折叠?



