
Installing and configuring a PostgreSQL server on your local macOS environment can significantly enhance your development workflow, allowing you to manage databases for your applications with ease. This blog post will guide you through setting up PostgreSQL on macOS Sonoma 14.3 using Homebrew, configuring it to launch at startup, and performing basic operations like checking the server status and connecting to your databases.
Homebrew is a popular package manager for macOS that simplifies the process of installing and managing software. To get started with PostgreSQL, ensure your Homebrew is up-to-date:
brew update
Next, install PostgreSQL:
brew install postgresql
To start the PostgreSQL service and have it run in the background:
brew services start postgresql@14
This command will initiate PostgreSQL as a background service, ensuring that it automatically starts whenever you boot your system.
macOS utilizes LaunchAgents to manage applications that start at login. To configure PostgreSQL to start automatically, you first need to create a directory for LaunchAgents if it doesn’t already exist:
mkdir -p ~/Library/LaunchAgents
Then, create a symbolic link for the PostgreSQL plist (property list) file to this directory:
ln -sfv /opt/homebrew/Cellar/postgresql@14/14.10_1/*.plist ~/Library/LaunchAgents
To register the PostgreSQL service with launchctl and ensure it starts at login for your user account:
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/homebrew.mxcl.postgresql@14.plist
launchctl enable gui/$(id -u)/homebrew.mxcl.postgresql@14.plist
These commands utilize your user ID to correctly place the PostgreSQL service under your user’s domain, automating its startup process.
To check whether the PostgreSQL server is running correctly, you can use the pg_ctl command with the specific directory of your PostgreSQL installation:
pg_ctl status -D /opt/homebrew/var/postgresql@14
This command provides immediate feedback on the server’s status, ensuring everything is operating as expected.
With your PostgreSQL server up and running, connect to the default database to start managing your data:
psql -d postgres
This command launches the psql command-line interface, allowing you to execute SQL commands, manage databases, and explore PostgreSQL features.
To view all databases on your PostgreSQL server:
\l
This command displays a list of all your current databases, their owners, and their encoding settings, resembling the following output:
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
mydb | myuser | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
(4 rows)
To exit the psql interface, simply type:
\q
Setting up PostgreSQL on macOS Sonoma 14.3 with Homebrew is a straightforward process that integrates seamlessly into your development environment. By following these steps, you can have a robust, fully functional PostgreSQL server ready for all your local development needs. Whether you’re working on web applications, exploring data science projects, or just learning SQL, a local PostgreSQL server is an invaluable tool for your development toolkit.