将PostgreSQL安装在Mac OS X Yosemite操作系统上

概述

以下是使用Homebrew将PostgreSQL安装到MacBook Air并记下的笔记。
只要按照这些最基本的步骤,应该就没问题了!只写了这么多。

步骤

用自制软件安装

我将在终端输入brew install postgres来安装。

~ $ brew install postgres
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/postgresql-9.4.0.yos
Already downloaded: /Library/Caches/Homebrew/postgresql-9.4.0.yosemite.bottle.tar.gz
==> Pouring postgresql-9.4.0.yosemite.bottle.tar.gz
==> Caveats
If builds of PostgreSQL 9 are failing and you have version 8.x installed,
you may need to remove the previous version first. See:
  https://github.com/Homebrew/homebrew/issues/2510

To migrate existing data from a previous major version (pre-9.4) of PostgreSQL, see:
  http://www.postgresql.org/docs/9.4/static/upgrading.html

To have launchd start postgresql at login:
    ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
Then to load postgresql now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Or, if you don't want/need launchctl, you can just run:
    postgres -D /usr/local/var/postgres
==> Summary
?  /usr/local/Cellar/postgresql/9.4.0: 2990 files, 40M

在展示的日志中,需要关注的是下面的部分。有简单的设置方法写着,差点错过了。

To have launchd start postgresql at login:
    ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
Then to load postgresql now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Or, if you don't want/need launchctl, you can just run:
    postgres -D /usr/local/var/postgres

只需使用此处提到的命令,即可完成最低限度的设置。

登录时的启动设置

基本上希望它始終保持运转,因此将执行以下出现的命令。

~ $ ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
/Users/<username>/Library/LaunchAgents/homebrew.mxcl.postgresql.plist -> /usr/local/opt/postgresql/homebrew.mxcl.postgresql.plist

启动程序

我对突然出现的LaunchAgents目录感到惊讶,所以简单查了一下。

launchd是一个开源的服务管理框架,用于启动、停止和管理守护程序、应用程序、进程和脚本。它是由苹果公司的Dave Zarzycki创建的,并在Mac OS X Tiger(Mac OS X v10.4)中引入。它在Apache许可证下发布。

总的来说,在Mac OS X上,有一个聪明的家伙可以帮助我们启动、停止和管理各种服务。我认为,在登录时设置它自动启动(创建符号链接)就是之前提到的脚本。

启动

因为我想立即使用,我会直接启动通过 lunchd 注册的 PostgreSQL。

~ $ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

结束了!已经完成了!现在可以使用了!太方便了吧!

尝试一下

我会试试看。

展示数据库列表

因为对有些东西感到好奇,所以首先尝试显示出来。

~ $ psql -l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  |  hoge    | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |
 template0 |  hoge    | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/hoge          +
           |          |          |             |             | hoge=CTc/hoge
 template1 |  hoge    | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/hoge          +
           |          |          |             |             | hoge=CTc/hoge
(3 rows)

登录后试试各种功能

登录

~ $ psql postgres
psql (9.4.0)
Type "help" for help.

创建表格

postgres=# create table users (id int, username text, email text);
CREATE TABLE

postgres=# \d
         List of relations
 Schema | Name  | Type  |  Owner
--------+-------+-------+----------
 public | users | table |  hoge
(1 row)

postgres=# \d users
      Table "public.users"
  Column  |  Type   | Modifiers
----------+---------+-----------
 id       | integer |
 username | text    |
 email    | text    |

插入数据并选择删除

postgres=# insert into users (id, username, email) values (1, 'hoge', 'hoge@hoge.com');
INSERT 0 1

postgres=# select * from users;
 id | username |     email
----+----------+---------------
  1 | hoge     | hoge@hoge.com
(1 row)

postgres=# truncate table users;
TRUNCATE TABLE

postgres=# select * from users;
 id | username | email
----+----------+-------
(0 rows)

删除表

postgres=# drop table users;
DROP TABLE
postgres=# \d
No relations found.

正在移动。好事好事。

总而言之 ér zhī)

在Mac OS X上,PostgreSQL可以通过以下3个步骤来使用!

    1. 安装Postgres:

 

    1. 1. 使用brew安装Postgres:

 

    1. “`bash

 

    1. brew install postgres

 

    1. “`

 

    1. 2. 创建符号链接以便在Launch Agents中加载Postgres:

 

    1. “`bash

 

    1. ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents

 

    1. “`

 

    1. 3. 加载Postgres服务:

 

    1. “`bash

 

    1. launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

 

    “`

轻松愉快。

bannerAds