Installing PostgreSQL on macOS with Homebrew
Introduction
PostgreSQL, a powerful open-source relational database management system, can be installed on macOS using Homebrew, a package manager for macOS.
Installation
- Install Homebrew by running:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Install PostgreSQL:
brew install postgresql
Starting, Stopping, and Restarting the Service
- Start PostgreSQL:
brew services start postgresql
- Stop PostgreSQL:
brew services stop postgresql
- Restart PostgreSQL:
brew services restart postgresql
Setting a Password
- Set a password for the default user
postgres
:
sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD 'new_password';"
Connecting to the Service
- Connect to PostgreSQL using the default user
postgres
and the password you set:
psql -U postgres -d postgres
Customizing the Configuration File
- Edit the configuration file
/usr/local/etc/postgresql/*.conf
:
sudo vi /usr/local/etc/postgresql/*.conf
- Make desired changes to the configuration parameters. For example, to change the listen address:
listen_addresses = '*'
Save and close the file.
Restart PostgreSQL to apply the changes:
brew services restart postgresql
Conclusion
Installing and configuring PostgreSQL on macOS using Homebrew is straightforward. By following these steps, you can easily set up a PostgreSQL database for your projects or applications. Remember to secure your database by setting a strong password and customizing the configuration file as needed.