简单介绍:Mac 上安装和使用PostgreSQL的方法

来源地址:https://www.jianshu.com/p/fedda9824f6a

 

两句话概括:

  1. 网络上的文档,因为各自的配置环境不同,所以可以用来参考,但不一定对自己适用;
  2. 官网文档要稍稍权威一点,介绍的要全面一点,今天深有感触;

简介

1.PostgreSQL: 是以加州大学伯克利分校计算机系开发的 POSTGRES,现在已经更名为PostgreSQL,版本 4.2为基础的对象关系型数据库管理系统(ORDBMS)。PostgreSQL支持大部分 SQL标准并且提供了许多其他现代特性:复杂查询、外键触发器、视图、事务完整性、MVCC。同样,PostgreSQL 可以用许多方法扩展,比如, 通过增加新的数据类型、函数、操作符、聚集函数、索引。免费使用、修改、和分发 PostgreSQL,不管是私用、商用、还是学术研究使用。

2.psql: 是PostgreSQL数据库的命令行交互工具

3.pgAdmin: 是PostgreSQL数据库的图形化管理工具

参考文献

老习惯,列出本文参考和引用的文档和博客,致以崇高的敬意,感兴趣的可以去看看
1.http://postgresapp.com/
2.http://postgresapp.com/documentation/cli-tools.html
3.http://www.yiibai.com/postgresql/2013080439.html

1.正文

公司项目连的都是远程数据库,安全起见,我还是弄个本地数据库,方便自己捣腾数据和后续的学习;
以前在linux(ubuntu 14.04)上,装过PostgreSql,还算熟悉。

1.1 安装postgresql

今天打算在Mac上安装PostgreSql以及它的图形化管理工具PgAdmin,方便对数据的查看和管理。
在Mac上安装PostgreSql,网络上很多方法都是通过homebrew来安装,命令也很简单

brew install postgresql 

但是好像还有另一种方法,就是使用官网提供的Postgres.app,
号称是:The easiest way to get started with PostgreSQL on the Mac。
看到这条口号,是不是有点心动,按照官网的提示来:

Quick Installation Guide
1.Download
2.Move to /Applications
3.Double Click

Done! You now have a PostgreSQL server running on your Mac. 
To use the command line programs, set up your $PATH. 
If you prefer a graphical app, check out the list of GUI tools.

If you get an error saying “the identity of the developer cannot be confirmed”, 
please make sure you didn’t skip step 2. (more info)

安装好运行,界面如下,非常简洁,点击Preferences可以看数据文件夹路径;

Postgres.app

The easiest way to get started with PostgreSQL on the Mac

Postgres.app is a full-featured PostgreSQL installation packaged as a standard Mac app. It includes everything you need to get started: we’ve even included popular extensions like PostGIS for geo data and plv8 for JavaScript.

Postgres.app has a beautiful user interface and a convenient menu bar item. You never need to touch the command line to use it – but of course we do include all the necessary command line tools and header files for advanced users.

Postgres.app updates automatically, so you get bugfixes as soon as possible.

The current version requires macOS 10.10 or later and comes with the latest PostgreSQL versions (10.3, 9.6.8, and 9.5.12), but we also maintain other versions of Postgres.app.

Installing Postgres.app

  • Download   ➜   Move to Applications folder   ➜   Double Click

    If you don't move Postgres.app to the Applications folder, you will see a warning about an unidentified developer and won't be able to open it.

  • Click "Initialize" to create a new server

  • Configure your $PATH to use the included command line tools (optional):

    sudo mkdir -p /etc/paths.d &&
    echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp

Done! You now have a PostgreSQL server running on your Mac with these default settings:

Host localhost
Port 5432
User your system user name
Database same as user
Password none
Connection URL postgresql://localhost

To connect with psql, double click a database. To connect directly from the command line, type psql. If you’d rather use a graphical client, see below.

NOTE: These instructions assume that you’ve never installed PostgreSQL on your Mac before. If you have previously installed PostgreSQL using homebrew, MacPorts, the EnterpriseDB installer, consider removing other PostgreSQL installations first. We also have instructions for upgrading from older versions of Postgres.app.

Graphical Clients

Postgres.app includes psql, a versatile command line client for PostgreSQL. But it’s not the only option; there are plenty of great graphical clients available for PostgreSQL. Two popular tools are:

pgAdmin 4 is a feature rich open source PostgreSQL client. It has support for almost every feature in PostgreSQL. The only downside is that the cross-plattform UI really doesn’t live up to the expectations of a native Mac app.

Postico on the other hand, is a very modern Mac app. It’s made by the same people that maintain Postgres.app, and we think you’ll like it! We put a lot of effort into making it a joy to use. However, it doesn’t have the extensive feature set of pgAdmin, and it’s a commercial app rather than open source.

Aside from those two options, there are a lot more to choose from! Check the documentation for a list of amazing Mac apps for PostgreSQL.

How to connect

After your PostgreSQL server is up and running, you’ll probably want to connect to it from your application. Here’s how to connect to PostgreSQL from popular programming languages and frameworks:

PHP
Python

To connect to a PostgreSQL server with Python, please first install the psycopg2 library:


pip install psycopg2
		

Django

In your settings.py, add an entry to your DATABASES setting:


DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql_psycopg2",
        "NAME": "[YOUR_DATABASE_NAME]",
        "USER": "[YOUR_USER_NAME]",
        "PASSWORD": "",
        "HOST": "localhost",
        "PORT": "",
    }
}
		

Flask

When using the Flask-SQLAlchemy extension you can add to your application code:


from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://localhost/[YOUR_DATABASE_NAME]'
db = SQLAlchemy(app)
		

SQLAlchemy


from sqlalchemy import create_engine
engine = create_engine('postgresql://localhost/[YOUR_DATABASE_NAME]')
		
Ruby
Java
C
Swift

Support

We have a list of common problems in the troubleshooting section in the documentation.

For general questions concerning PostgreSQL, have a look at the official PostgreSQL documentation.

If you have a question concerning Postgres.app that is not answered by the Postgres.app documentation, you can ask @PostgresApp on Twitter, or open an issue on GitHub.

When reporting bugs, let us know which version of Postgres.app & macOS you are using, and be sure to include detailed error messages, even if your issue seems similar to another one.

License

Postgres.app, PostgreSQL, and its extensions are released under the PostgreSQL License. The released binaries also include OpenSSL (OpenSSL License), PostGIS (GPLv2), and plv8 (3 clause BSD).

Postgres.app is maintained by Jakob Egger and Chris Pastl. It was originally created by Mattt Thompson.

 



作者:严三金
链接:https://www.jianshu.com/p/fedda9824f6a
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
posted @ 2018-03-12 20:40  会飞的加菲猫  阅读(5919)  评论(0编辑  收藏  举报