Mac Postgres App Psql
There are many clients for PostgreSQL on the Mac. You can find many of them in the Community Guide to PostgreSQL GUI Tools in the PostgreSQL wiki. Some of them are quite powerful; some are still a bit rough. Here’s a list of all the Mac Apps I found (in alphabetic order): Datagrip; Datazenit; DBeaver; DBGlass; DbVisualizer; Froq; Metabase; Navicat for PostgreSQL. If you are using Postgres.app, you can find the Data Directory location in the Preferences dialog box. In that directory, the log is at postgres-server.log. For example, on my machine, the log is at /Users/nofinator/Library/Application Support/Postgres/var-9.4/postgres-server.log. The Postgres App (above) must be running to enter the shell. There is a help menu for PostgreSQL available in the base terminal and the PSQL shell. The terminal help menu can be accessed by typing.
The easiest way to install PostgreSQL on a Mac is by installing Postgres.app. Postgres.app is a full-featured PostgreSQL installation packaged as a standard Mac application. The easiest way to install Postgres on a Mac is to visit the Postgres.app downloads page and get the latest stable version of PostgreSQL as a DMG interactive installer. Once the download is complete, navigate to your Downloads directory in a Finder window and then double-click on.
This is a tutorial for setting up PostgreSQL on MacOS. You might be wondering why there's a need for another setup guide for Postgres in the first place, since there are a few across the web. I found many tutorials and guides on how to set it up, but found most of them to be unclear and outdated, based on old PostgreSQL versions. The checklist is not comprehensive, though, as it is used for several tutorials on my website, I keep it updated with the recent versions of PostgreSQL. If you spot any flaws in this guide, a comment below would be very helpful to keep it up to date for other developers.
If you want to run PostgreSQL on Windows instead, you will find guidance over here: How to setup PostgreSQL on Windows.
PostgreSQL Installation on MacOS
I recommend Homebrew for installing and managing applications on MacOS. It is installed using the following command in the MacOS terminal:
The terminal runs through a series of installation operations, and will probably create folders in your local machine to accommodate Homebrews storage requirements. You can find more detailed instructions here. After it's installed, update the Homebrew dependencies and install PostgreSQL on the command line:
Next, check your PostgreSQL version:
The command line results will show the version you have installed on your local machine. I recommed using the latest version of libraries and software whenever possible to avoid compatibility issues with client-side applications.
How to create a physical PostgreSQL Database
Now you can initialize the physical space on your hard-disk to allocate databases. To do this, create a default postgres database on the command line in case it didn't happen automatically:
You will see the error message: 'initdb: directory '/usr/local/var/postgres' exists but is not empty' if the database was already created when you installed PostgreSQL. It means the folder where you are attempting to create a physical place for the database already has one. Either way, next you can move on to the next step.
When you connect to this physical database later, you will see an actual database which is called 'postgres' as well. The postgres database is meant to be the default database for any third-party tools that you are using in combination with PostgreSQL. These tools attempt to make the default connection to this default database, so you shouldn't delete it.
How to start/stop a PostgreSQL Database
Let's see next how you can interact with the actual database. Manually start and stop your Postgres database server with the following commands:
The terminal will confirm these operations with 'server started' and 'server stopped' feedback. You could also implement a script to start the server each time you boot up the machine, but I like to have control over when to start and stop my database server to avoid complications.
How to create the actual PostgreSQL Database
Next, let's go through the steps of setting up a database that can be used for one of your applications. Make sure the Postgre server is started first, then type these commands in the command line to create and remove a database:
You can also connect to databases to execute SQL statements. Either use the psql
command, or specify a database such as the default postgres database to connect:
The command leads you to the psql shell, which you can exit by typing CTRL + d. In the psql shell, you can create and drop databases as well:
To list all your databases, you can type list
. Your will see any new databases listed, as well as two default databases that come with postgreSQL called template0
and template1
. The templates should remain in your database list even if you aren't using them, as they may be useful later.
- list - List all of your actual databases.
- c mydatabasename - Connect to another database.
- d - List the relations of your currently connected database.
- d mytablename - Shows information for a specific table.
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 can install minor updates automatically, so you get bugfixes as soon as possible.
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):
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:
To connect from PHP, make sure that it supports PostgreSQL. The version included with macOS doesn't support PostgreSQL. We recommend MAMP for an easy way to install a current version of PHP that works.
You can use PDO (object oriented):
Or the pg_connect() functions (procedural):
To connect to a PostgreSQL server with Python, please first install the psycopg2 library:
Django
In your settings.py, add an entry to your DATABASES setting:
Flask
When using the Flask-SQLAlchemy extension you can add to your application code:
SQLAlchemy
To install the pg gem, make sure you have set up your $PATH correctly (see Command-Line Tools), then execute the following command:
Rails
In config/database.yml, use the following settings:
Sinatra
In config.ru or your application code:
ActiveRecord
Install the activerecord gem and require 'active_record', and establish a database connection:
DataMapper
Install and require the datamapper and do_postgres gems, and create a database connection:
Sequel
Install and require the sequel gem, and create a database connection:
- Download and install the PostgreSQL JDBC driver
- Connect to the JDBC URL jdbc:postgresql://localhost
For more information see the official PostgreSQL JDBC documentation.
Mac Postgres.app
libpq is the native C client library for connecting to PostgreSQL. It's really easy to use:
Now compile the file with clang and run it:
You can just use the C API in Swift! First include libpq in your bridging header:
Then make sure to link with libpq.
On iOS, you'll need to build libpq yourself.
On macOS you can use the system provided libpq (does not support SSL) or use libpq provided by Postgres.app by adding the following build settings:
Other Linker Flags | -lpq |
---|---|
Header Search Paths | /Applications/Postgres.app/Contents/Versions/latest/include |
Library Search Paths | /Applications/Postgres.app/Contents/Versions/latest/lib |
Install Psql Mac
Now you can use the libpq C library to connect to PostgreSQL:
Support
We have a list of common problems in the troubleshooting section in the documentation.
Mac Postgres Command Line
For general questions concerning PostgreSQL, have a look at the official PostgreSQL documentation.
Install Postgres Mac
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).
Mac Psql
Postgres.app is maintained by Jakob Egger. It was originally created by Mattt Thompson.