Brew Start Postgresql



Start/Stop PostgreSQL Without Homebrew In the event that you don’t have (or don’t wish to use) Homebrew for Postgres, yet you have Postgres installed already, you can also manually start it with pgctl, which is the launch utility provided by Postgres itself. This will start Postgres (assuming default directories). Brew install postgres. Step 2: Start postgres server. Brew services start postgresql. Step 3: Create a database. Lets call our database “book” $ createdb book. Step 4: open the psql shell. List all databases in terminal with: $ psql -list. Adding the name of the db after the command psql will open the shell to that db $ psql book.

Install PostgreSQL via Homebrew

Likely most OS X users know of Homebrew, but Homebrew is a package manager for OS X that allows you to easily install and run a massive library of applications and utilities with ease.

We won’t go through the entire (albeit short) installation process of Homebrew, but if you wish to install Homebrew and use it for PostgreSQL management, check out the installation command on the official documentation. If you aren’t sure if Homebrew is installed, try the which brew command from the terminal to verify.

Once Homebrew is installed, you can install PostgreSQL by issuing the following commands in your terminal:

The first two commands are used to update Homebrew and report any potential issues (if necessary). Then, of course, brew install postgres is the one-line command for installing PostgreSQL.

You should see a good deal of useful information in the output during installation, much of which should be copied down for use in the next sections.

Using a LaunchAgent and plist to Launch PostgreSQL on Startup

In most cases, you’ll likely wish to have PostgreSQL launch when you start up your system, so you’ll need to tell your computer this is your desire.

First, you’ll need to create a directory for your LaunchAgents to reside (if the directory doesn’t exist already). LaunchAgents in OS X are simple scripts used by launchd that cause the system to run programs or code during startup.

Create your user-specific LaunchAgents directory with this command, if necessary:

Now you’ll need to create a symbolic link from the script that actually allows Postgres to run to the LaunchAgents directory. A symbolic link is similar to creating a new copy of a file for use in another directory, but since the link is ‘symbolic’, the link is just a forwarding address: any request made to that symbolic link location is actually “forwarded along” or redirected to where the real file actually resides.

Link to the plist (property list) file that was generated by Homebrew and place that new symbolic link in LaunchAgents with this command:

Note: Double-check that the command is correct: It should’ve been part of the installation output mentioned above when Homebrew installed Postgres initially.

Finally, we load the new symbolic link’ed LaunchAgent file using the launchctl load command, which is specifically what informs the computer to run this script and start Postgres when the computer launches. Again, the exact command to enter for your own installation will be an output during Homebrew’s Postgres installation, but it should look something like this:

Manually Starting PostgreSQL

To manually startup Postgres without rebooting, again you should be able to use the command that was output during the installation, like so:

This will attempt to launch Postgres in daemon mode, which means it will be running as a background process without taking over your terminal.

Start/Stop PostgreSQL Without Homebrew

In the event that you don’t have (or don’t wish to use) Homebrew for Postgres, yet you have Postgres installed already, you can also manually start it with pg_ctl, which is the launch utility provided by Postgres itself.

This will start Postgres (assuming default directories):

And this will stop Postgres:

Startup Troubleshooting: Did You Run initdb?

In some cases, if you have trouble running Postgres, be sure you’ve executed the initdb command one time, which causes Postgres to initialize the database cluster for a new installation and allows you to connect with the default postgres user.

Before anyone can access the database, you must start the database server. The database server program is called postgres. The postgres program must know where to find the data it is supposed to use. This is done with the -D option. Thus, the simplest way to start the server is:

Macos Restart Postgres

which will leave the server running in the foreground. This must be done while logged into the PostgreSQL user account. Without -D, the server will try to use the data directory named by the environment variable PGDATA. If that variable is not provided either, it will fail.

Normally it is better to start postgres in the background. For this, use the usual Unix shell syntax:

It is important to store the server's stdout and stderr output somewhere, as shown above. It will help for auditing purposes and to diagnose problems. (See Section 23.3 for a more thorough discussion of log file handling.)

The postgres program also takes a number of other command-line options. For more information, see the postgres reference page and Chapter 18 below.

Brew

This shell syntax can get tedious quickly. Therefore the wrapper program pg_ctl is provided to simplify some tasks. For example:

will start the server in the background and put the output into the named log file. The -D option has the same meaning here as for postgres. pg_ctl is also capable of stopping the server.

Normally, you will want to start the database server when the computer boots. Autostart scripts are operating-system-specific. There are a few distributed with PostgreSQL in the contrib/start-scripts directory. Installing one will require root privileges.

Different systems have different conventions for starting up daemons at boot time. Many systems have a file /etc/rc.local or /etc/rc.d/rc.local. Others use init.d or rc.d directories. Whatever you do, the server must be run by the PostgreSQL user account and not by root or any other user. Therefore you probably should form your commands using su postgres -c '...'. For example:

Brew Start Postgresql

Here are a few more operating-system-specific suggestions. (In each case be sure to use the proper installation directory and user name where we show generic values.)

  • For FreeBSD, look at the file contrib/start-scripts/freebsd in the PostgreSQL source distribution.

  • On OpenBSD, add the following lines to the file /etc/rc.local:

  • On Linux systems either add

    to /etc/rc.d/rc.local or /etc/rc.local or look at the file contrib/start-scripts/linux in the PostgreSQL source distribution.

  • On NetBSD, use either the FreeBSD or Linux start scripts, depending on preference.

  • On Solaris, create a file called /etc/init.d/postgresql that contains the following line:

    Then, create a symbolic link to it in /etc/rc3.d as S99postgresql.

While the server is running, its PID is stored in the file postmaster.pid in the data directory. This is used to prevent multiple server instances from running in the same data directory and can also be used for shutting down the server.

17.3.1. Server Start-up Failures

There are several common reasons the server might fail to start. Check the server's log file, or start it by hand (without redirecting standard output or standard error) and see what error messages appear. Below we explain some of the most common error messages in more detail.

Brew Start Postgresql

This usually means just what it suggests: you tried to start another server on the same port where one is already running. However, if the kernel error message is not Address already in use or some variant of that, there might be a different problem. For example, trying to start a server on a reserved port number might draw something like:

A message like:

probably means your kernel's limit on the size of shared memory is smaller than the work area PostgreSQL is trying to create (4011376640 bytes in this example). Or it could mean that you do not have System-V-style shared memory support configured into your kernel at all. As a temporary workaround, you can try starting the server with a smaller-than-normal number of buffers (shared_buffers). You will eventually want to reconfigure your kernel to increase the allowed shared memory size. You might also see this message when trying to start multiple servers on the same machine, if their total space requested exceeds the kernel limit.

An error like:

does not mean you've run out of disk space. It means your kernel's limit on the number of System V semaphores is smaller than the number PostgreSQL wants to create. As above, you might be able to work around the problem by starting the server with a reduced number of allowed connections (max_connections), but you'll eventually want to increase the kernel limit.

If you get an 'illegal system call' error, it is likely that shared memory or semaphores are not supported in your kernel at all. In that case your only option is to reconfigure the kernel to enable these features.

Details about configuring System VIPC facilities are given in Section 17.4.1.

17.3.2. Client Connection Problems

Although the error conditions possible on the client side are quite varied and application-dependent, a few of them might be directly related to how the server was started. Conditions other than those shown below should be documented with the respective client application.

This is the generic 'I couldn't find a server to talk to' failure. It looks like the above when TCP/IP communication is attempted. A common mistake is to forget to configure the server to allow TCP/IP connections.

Alternatively, you'll get this when attempting Unix-domain socket communication to a local server:

Brew Start Postgresql Command

The last line is useful in verifying that the client is trying to connect to the right place. If there is in fact no server running there, the kernel error message will typically be either Connection refused or No such file or directory, as illustrated. (It is important to realize that Connection refused in this context does not mean that the server got your connection request and rejected it. That case will produce a different message, as shown in Section 19.4.) Other error messages such as Connection timed out might indicate more fundamental problems, like lack of network connectivity.