簡單介紹: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
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM