Skip to main content

You Can Install PostgreSQL with conda?

· 5 min read
Travis Hathaway
Conda maintainer 👷🔧
Banner image for You Can Install PostgreSQL with conda? blog post

Part 1 of the "You Can Do That with conda?" series—exploring unexpected capabilities of conda beyond Python packages.

Conda has long been the driver of data science workflows because of its unique ability to manage the complexities around Python packaging's diverse dependency requirements. It's precisely because of this that conda is also able to handle managing much more than just Python dependencies.

In this tutorial, we'll show the strengths of conda's flexibility and provide a guide on how you can install PostgreSQL for local development environments. Installing PostgreSQL this way offers several advantages: no root or admin permissions are required, the installation is isolated and reproducible, and your database can be version-controlled alongside other project dependencies—making it a lighter-weight alternative to container-based solutions like Docker.

Prerequisites​

Throughout this tutorial, we'll show you how to install and run PostgreSQL from conda-forge using either conda (or mamba/micromamba) or pixi. Please refer to those projects' documentation for information on how to install them.

Setting up your environment​

To start, we'll create a new environment with the postgresql package.

To create a new conda environment with PostgreSQL, run the following command:

conda create --name postgresql --channel conda-forge postgresql
tip

You can substitute conda with mamba or micromamba in the command above.

Running PostgreSQL​

Now that we have our environment set up and packages installed, we can initialize our database and get it running. The postgresql package exposes the following commands that we'll use to do that:

  • initdb
  • createuser
  • createdb
  • pg_ctl

Creating a data directory​

The first thing we need to do is create the directory we'll use to store our data. For this example, we're going to create a "postgresql" directory in the home directory, but this could be anywhere you'd like:

mkdir ~/postgresql
initdb -D ~/postgresql

Starting the server​

Now that we have a location set for storing our data, we can launch the postgresql process by running the following command:

pg_ctl -D ~/postgresql -l ~/postgresql/logfile start

With -D, we pass it the location of our data directory and the -l option sets the file path that will receive postgresql's log output.

Creating a user and a database​

The next two commands will create a user account and database we can use for our application:

createuser user
createdb --owner user user

After this, your new database should be ready to accept connections. You can test this by running the following psql command:

psql -U user
warning

At this point, the database accepts connections from localhost without passwords. In postgresql's configuration, this is referred to as "trust". This is only advisable if you are the only person using your computer. Read further to learn how to adjust this.

Editing the configuration​

To make changes to how the server is configured, you can edit the default configuration that was created with the initdb command. The two most important files for postgresql's configuration are:

  • pg_hba.conf: controls authentication
  • postgresql.conf: all other configuration settings
Where are these files?

These configuration files are located in the data directory you specified with initdb (in our example, ~/postgresql/). The pg_hba.conf file controls "host-based authentication"—which users can connect from which hosts and how they authenticate.

For example, if you wanted to change the default authentication behavior of your postgresql server, you could edit the lines at the bottom of the pg_hba.conf to use password instead of trust:


# TYPE DATABASE USER ADDRESS METHOD

# "local" is for Unix domain socket connections only
local all all password
# IPv4 local connections:
host all all 127.0.0.1/32 password
# IPv6 local connections:
host all all ::1/128 password
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all password
host replication all 127.0.0.1/32 password
host replication all ::1/128 password

Afterwards, you would need to restart the server with the following command:

pg_ctl -D ~/postgresql -l ~/postgresql/logfile restart

Final thoughts​

We hope this gave you a good orientation on using PostgreSQL via conda! Installing PostgreSQL this way offers several practical benefits:

  • No root permissions required: You can set up a full database server in user space.
  • Reproducible environments: Include postgresql in your environment.yml or pixi.toml and share the same setup across your team.
  • Lighter-weight than Docker: For local development, this approach avoids the overhead of running a container runtime.
  • Version control friendly: Your database dependency can be locked alongside your application dependencies.

Because you can install PostgreSQL this way, it also opens up possibilities for bundling applications that use PostgreSQL with conda. If you want to extend this setup, consider adding packages like postgis for geospatial support or psycopg2 for Python database connectivity—all available from conda-forge.

Further reading​