Postgres Installation
If you already have a local installation of Postgres running on your machine, you can skip this section.
Setting up Postgres on Mac using Brew
This guide will walk you through the process of setting up Postgres on a Macbook using Brew. For other methods please see the Postgres Documentation.
Prerequisites
Before proceeding with this guide, ensure that you have the following prerequisites:
- A Macbook with MacOS installed
- A working internet connection
- Basic knowledge of using the terminal
Installation
Open Terminal on your Macbook.
Install Homebrew package manager by running the following command in your terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Once Homebrew is installed, use the following command to install Postgres:
brew install postgresql
This will download and install Postgres and its dependencies.
After installation, start the Postgres service by running the following command:
brew services start postgresql
This will start the Postgres service and ensure that it starts automatically on boot.
Creating a Default Postgres User and Password
Open Terminal and type the following command to access the Postgres command line interface:
psql postgres
Once you are in the Postgres command line interface, type the following command to create a new user:
CREATE USER `enteryourdesiredusernamehere` WITH PASSWORD 'yourpassword';
Replace enteryourdesiredusernamehere
and yourpassword
with your desired username and password.
Once the user is created, you can grant the user access to a database using the following command:
GRANT ALL PRIVILEGES ON DATABASE yourdatabasename TO yourusername;
Replace yourdatabasename
and yourusername
with your desired database name and username.
Congratulations! You have now successfully set up Postgres on your Macbook using Brew and created a default user and password.
Installing Postgres on Windows
Prerequisites
Before proceeding with this guide, ensure that you have the following prerequisites:
- A Windows computer running Windows 7 or later
- A working internet connection
- Administrator access to the computer
- Basic knowledge of using the command prompt
Installation
Download the Postgres installer from the Postgres website.
Double-click the downloaded installer file to begin the installation process.
Select the components you want to install and click "Next".
Choose the directory where you want to install Postgres and click "Next".
On the "Password" screen, set a password for the postgres user and click "Next".
On the "Port" screen, choose the port number to use for Postgres and click "Next".
On the "Advanced Options" screen, leave the default options as they are, and click "Next".
On the "Ready to Install" screen, click "Next" to start the installation process.
Once the installation process is complete, click "Finish".
Open the Command Prompt and navigate to the directory where Postgres is installed. The default location is
C:\Program Files\PostgreSQL\version\bin
, whereversion
is the version of Postgres you installed.Type the following command to start the Postgres service:
pg_ctl.exe start -D "C:\Program Files\PostgreSQL\version\data"
This will start the Postgres service.
To verify that Postgres is installed and working correctly, type the following command:
psql -U postgres
You will be prompted to enter the password you set for the postgres user during the installation process.
If everything is working correctly, you should see the Postgres command prompt.
Congratulations! You have now successfully installed Postgres on your Windows computer.
Installing Postgres on Linux
Prerequisites
Before proceeding with this guide, ensure that you have the following prerequisites:
- A Linux computer running a supported distribution. For a comprehensive list of the supported platforms please see the Postgres docs
- A working internet connection
- Administrator access to the computer
- Basic knowledge of using the command line
Installation
The installation process for Postgres varies depending on your Linux distribution. Here are the installation instructions for some popular distributions:
Ubuntu and Debian
Open a terminal window.
Update the package list by running the following command:
sudo apt-get update
Install Postgres by running the following command:
sudo apt-get install postgresql
Once the installation process is complete, the Postgres service should start automatically. You can verify that the service is running by running the following command:
sudo systemctl status postgresql
CentOS and Fedora
Open a terminal window.
Install the Postgres repository by running the following command:
sudo dnf install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Install Postgres by running the following command:
sudo dnf install postgresql12-server postgresql12
Initialize the database by running the following command:
sudo /usr/pgsql-12/bin/postgresql-12-setup initdb
Once the initialization process is complete, start the Postgres service by running the following command:
sudo systemctl start postgresql-12
You can verify that the service is running by running the following command:
sudo systemctl status postgresql-12
Setting up the Postgres User
Open a terminal window.
Switch to the postgres user by running the following command:
sudo su - postgres
Once you are logged in as the postgres user, run the following command to access the Postgres command line interface:
psql
Once you are in the Postgres command line interface, run the following command to set a password for the postgres user:
\password postgres
Type in the desired password and press Enter.
Once the password is set, exit the Postgres command line interface by running the following command:
\q
Congratulations! You have now successfully installed Postgres on your Linux computer and set up the postgres user.