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.
- To have launchd start postgresql now and restart at login: brew services start postgresql Or, if you don't want/need a background service you can just run: pgctl -D /usr/local/var/postgres start $ But if you do want to use 10.1, basically you could run something like this, still assuming you were running 9.6.6 before (change directory name.
- PostgreSQL is a powerful, open source object-relational database system with over 30 years of active development that has earned it a strong reputation for reliability, feature robustness, and performance.
- Alias start-pg='brew services start postgresql' alias stop-pg='brew services stop postgresql' alias restart-pg='brew services restart postgresql' Share. Improve this answer. Follow answered Oct 15 '20 at 17:44. Raghuram Raghuram. 21 1 1 bronze badge.
- In our previous post, we installed PostgreSQL via Homebrew and got our own little cluster up and running under our userid. That is probably good enough for 90% of the users out there who just want to play with or devel on PostgreSQL, but for those of us who need a little more flexibility in our installs, we’re going to take the next step.
I'm using brew too. As of now, the datadir listed in pgconfig -configure is not the same as the one brew info postgresql recommends (which is $(brew -prefix)/var/postgres and is not a symlink to the one listed). It seems the formula is a bit lax on using config flags properly. – Vaz Aug 12 '15 at 21:52.
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.
Homebrew Install Postgres
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.
This installation process is from MONCEF BELYAMANI
This is a quick guide for installing PostgreSQL (Postgres
for short) on a Mac with Homebrew, and starting and stopping it with Lunchy. If you've just forked a Ruby on Rails project that requires Postgres, this tutorial will get you up and running in no time.
If you're working on a brand new Rails app and need to create a new Postgres user and database for your app, I recommend Will Jessop's tutorial or Peter Cooper's screencast.
Step 1: Install Homebrew
To make sure you have all the tools necessary for Ruby on Rails development, I recommend you follow my tutorial for installing Xcode, Homebrew, Git, RVM, and Ruby on a Mac.
Step 2: Update Homebrew
Before you install anything with Homebrew, you should always make sure it's up to date and that it's healthy:
Step 3: Install Postgres
When you install Postgres, you will see a bunch of output in your Terminal that looks like this:
The important sections are Build Notes
, Create/Upgrade a Database
, and Start/Stop PostgreSQL
. Make sure to follow those instructions to the letter.
Step 4: Create/Upgrade a database
If this is your first time installing Postgres with Homebrew, you'll need to create a database with:
I copied and pasted that command directly from the Terminal output. You should do the same, in case the instructions change in the future.
Step 5: Install Lunchy
Lunchy is a helpful gem that will allow you to easily start and stop Postgres.
Step 6: Start/Stop Postgres
Once again, I'm copying and pasting the following commands from my Terminal. Note that the second command is specific to the version of Postgres that was installed on my machine. If the version number has changed since I've written this tutorial, using the command below won't work for you, so you should make sure to copy the command from your Terminal output.
Since we're using Lunchy, we don't need to run this third command:
Instead, we'll simply use this to start Postgres:
Postgresql Brew Table
At this point, you should be all set to run the rake
commands to create and use the database for the existing Rails app you're working on.
Postgresql Brewing Company
To stop Postgres: