October 11, 2024
Installing PostgreSQL
Before using PostgreSQL, you'll need to install it on your system. These instructions offer a guide to getting it running on Linux, Windows, macOS, and Docker.
For all platforms, the first place you can check is the PostgreSQL downloads page to see if your OS has an installer or package available.
The most common and straightforward installation method is usually using the package manager for your OS, as long as you are using a stable release of PostgreSQL.
On Linux (Ubuntu)
On Linux, you can install PostgreSQL using the package manager for your specific distribution. For Ubuntu, you can use the following command to install the latest stable version of PostgreSQL, which is 16 as of recording.
sudo apt update
sudo apt install postgresql
# It's often useful to also install postgresql-contrib for additional tools and extensions
sudo apt install postgresql-contrib
You can check if the PostgreSQL service is running with the following command:
sudo systemctl status postgresql
The server configuration file is located at /etc/postgresql/16/main/postgresql.conf
. (If you're using a different version, replace 16 with the version number.)
On Windows
For many purposes, Windows Subsystem for Linux (WSL) is the best way to get PostgreSQL on Windows. You can install WSL on Windows 10 or later and then follow the instructions for Linux above.
wsl --install
You may need to reboot. Once WSL is installed, you can install PostgreSQL using the package manager for your specific distribution. For Ubuntu, refer to the instructions above.
However, if you want to install the Windows version of PostgreSQL, you can download the installer from the PostgreSQL downloads page and run it. (EDB provides the installers.)
By default, PostgreSQL will be installed in C:\Program Files\PostgreSQL\<version>\
, and the server will be started automatically.
You can find the server configuration file at C:\Program Files\PostgreSQL\<version>\data\postgresql.conf
.
On macOS
There are several ways to install PostgreSQL on macOS. The easiest is to use Homebrew. Refer to the https://brew.sh/ for instructions on installing Homebrew which involves running /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
from the command line (terminal) app.
brew update
brew install postgresql
When installed via Homebrew, the server configuration file is located at /usr/local/var/postgres/postgresql.conf
.
Currently, the latest stable version is 16, and 17 isn't yet available via Homebrew.
The configuration file is typically located at /usr/local/var/postgresql@VERSION/postgresql.conf
.
To confirm the configuration file location, run the following:
SHOW config_file;
Alternatively, you can download the installer via the PostgreSQL downloads page from https://www.enterprisedb.com/downloads/postgres-postgresql-downloads and run it.
When installed through this method, the server configuration file should be at /Library/PostgreSQL/<version>/data/postgresql.conf
.
If this isn't the case, you can try running
/Library/PostgreSQL/<version>/bin/psql -U postgres -c "SHOW config_file;"
to find out where the configuration file is located.
On Docker
Docker is a great way to run PostgreSQL without installing it directly on your system. You can install Docker Desktop from the Docker website.
Once Docker is installed, you can pull and run PostgreSQL via the Docker Desktop app by searching "postgres" and pulling the version you want (e.g., 16.4, 17rc1, etc).
You can also pull it via the command line with the following command:
docker pull postgres # You can also specify a version, e.g., docker pull postgres:16
To start a container, you can use the following command (change the name and password as desired):
docker run --name my-postgres-name -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres
This command does the following:
- Creates a container named
my-postgres
- Sets the PostgreSQL superuser password to
mysecretpassword
- Maps the container's 5432 port to the host's 5432 port
- Runs the container in detached mode (-d)
-
The PostgreSQL data is stored inside the container. If you want to persist the data on the host system, you can use a volume:
docker run --name my-postgres-name -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -v pgdata:/var/lib/postgresql/data -d postgres
This command does the following:
- Creates a container named
my-postgres-name
- Sets the PostgreSQL superuser password to
mysecretpassword
- Maps the container's 5432 port to the host's 5432 port
- Mounts the specified directory on the host to the container's
/var/lib/postgresql/data
directory -
-d
runs the container in detached mode (in the background)
Conclusion
If you were able to follow any of the above methods, you should now have PostgreSQL installed on your system. You can now start using it to create databases, tables, and run queries, either through the command like psql conole or your favorite GUI admin tool like TablePlus.
If you have any issues or find errors in this documentation, you can reach out to us at support@aaronfrancis.com