Skip to content

Instantly share code, notes, and snippets.

@gwangjinkim
Last active February 17, 2026 10:06
Show Gist options
  • Select an option

  • Save gwangjinkim/f13bf596fefa7db7d31c22efd1627c7a to your computer and use it in GitHub Desktop.

Select an option

Save gwangjinkim/f13bf596fefa7db7d31c22efd1627c7a to your computer and use it in GitHub Desktop.
How to install and run postgresql in conda
This gist I write, because I couldn't find any step by step instructions
how to install and start postgresql locally (using conda within a conda environment - with the result
that you can run it without sudo/admin rights on your machine!)
and not globally in the operating system (which requires sudo/admin rights on that machine).
I hope, this will help especially people new to postgresql
(and those who don't have sudo/admin rights on a specific machine but want
to run postgresql there)!
Prerequisite is to have miniconda already installed in your Computer.
In Windows, we use Scoop to install miniconda.
Installing minicionda as well as Scoop doesn't require sudo/admin rights on your machine.
This is the beauty of conda and scoop. You can completely use it as a non-admin.
And that is also the reason why I show you how to install and run postrgesql locally - without
sudo / admin rights here.
####################################
# install miniconda
####################################
# Following the instructions [here](https://www.anaconda.com/docs/getting-started/miniconda/install#quickstart-install-instructions)
# In MacOS/Linux:
mkdir -p ~/miniconda3
curl https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh -o ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm ~/miniconda3/miniconda.sh
source ~/miniconda3/bin/activate # initialize conda
conda init --all
# In Windows PowerShell
# Following https://github.com/ScoopInstaller/Scoop
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser # install scoop first
Invoke-RestMethod -Uri https://get.scoop.sh | Invoke-Expression
scoop bucket add extras # the bucket 'extras' is needed for miniconda!
scoop install miniconda
# scoop is for all future installations as a developer super useful
# it makes installations in windows more like linux installations
# alternatively: You can in Windows also activate WSL2, install Ubuntu
# (Windows Subsystem of Linux)
# and then in the Ubuntu shell follow the Linux instructions in your PowerShell
# also in PowerShell after installation, initilize conda by:
conda init --all
# with conda (in this case miniconda and not anaconda) installed,
# you can now follow the rest of the installations
####################################
# create conda environment
####################################
# in conda, you create a new conda environment by:
conda create --name myenv
# myenv is just an example environment name - use your name of preference!
# enter the environment
conda activate myenv
####################################
# install postgresql via conda
####################################
conda install -y -c conda-forge postgresql
####################################
# create a base database locally
####################################
initdb -D mylocal_db
##############################
# now start the server modus/instance of postgres
##############################
pg_ctl -D mylocal_db -l logfile start
## waiting for server to start.... done
## server started
# now the server is up
####################################
# create a non-superuser (more safety!)
####################################
createuser --encrypted --pwprompt mynonsuperuser
# asks for name and password
####################################
# using this super user, create inner database inside the base database
####################################
createdb --owner=mynonsuperuser myinner_db
####################################
# in this point, if you run some program,
# you connect your program with this inner database
# e.g. Django
####################################
# in django (Python) e.g. you open with your favorite editor:
nano <mysite>/settings.py # or instead of nano your favorite editor!
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'myinner_db',
'USER': 'mynonsuperuser',
'PASSWORD': '<mynonsuperuserpassword>',
'HOST': 'localhost',
'PORT': '',
}
}
# and it is available for django
# so that you can do
##############################
# do with the connected program these further steps
##############################
# first install psycopg2
# because django requires this for handling postgresql
conda install -c anaconda psycopg2
# then now you can do:
python manage.py migrate
# to fully integreate the postgresql into your django website
# and to be able to use the database, you also need to create a superuser
python manage.py createsuperuser --username name
################################
# stop running the postgres instance under ubuntu
################################
# monitor whether a postgres instance/server is running or not
ps aux | grep postgres
# if no instance is running, you will see only one line as the answer to your query - which is from your grep search!
# ending with: grep --color=auto postgres
# ignore this line!
# if an instance of postgresql server is running, then several
# processes are runnng
# you can kill the server by the first number of the leading line!
kill <number>
# e.g. the output of `ps aux | grep postgres` was:
# username 2673 0.0 0.0 14760 512 pts/11 S+ 07:34 0:00 grep --color=auto postgres
# username 30550 0.0 0.0 179144 18996 ? S Jun13 0:01 /home/username/miniconda3/envs/django/bin/postgres -D mylocal_db
# username 30552 0.0 0.0 179276 4756 ? Ss Jun13 0:00 postgres: checkpointer process
# username 30553 0.0 0.0 179144 5216 ? Ss Jun13 0:01 postgres: writer process
# username 30554 0.0 0.0 179144 8464 ? Ss Jun13 0:01 postgres: wal writer process
# username 30555 0.0 0.0 179700 5792 ? Ss Jun13 0:01 postgres: autovacuum launcher process
# username 30556 0.0 0.0 34228 3416 ? Ss Jun13 0:03 postgres: stats collector process
# then # 2673 is just the 'grep --color=auto postgres' so ignore
# the line ending with 'postgres -D /path/to/mylocal_db' is the leading line!
# take first number occuring in this line (PID - process ID number) which is 30550, therefore kill it by:
kill 30550
####################################
# run postgres as a non-server in the background
####################################
postgres -D db_djangogirls & # runs postgres
# press RET (return) to send it to background!
# you can stop and switch to server mode by
# following 'stop running postgres instance under ubuntu'
##############################
# stop non-server or server modus/instance of postgres
##############################
ps aux | grep postgres # see detailed instructions for finding the correct <process ID>
# under 'stop running postgres instance under ubuntu'! And then do:
kill <process ID> # to stop postgres
Have fun with your completely locally running - more safe - postgresql!!!
@wybert
Copy link

wybert commented Apr 18, 2022

By running pg_ctl -D mylocal_db -l logfile start, is the database server running all the time? How about after restarting the machine. Is they any ways to set up a systemctl services here?

@yogenderPalChandra
Copy link

Thanks, I had to come over to these instructions yet once again...

@chintani
Copy link

chintani commented Jun 4, 2022

Thanks! Finding this saved me from a lot of headache ;-)

@gwangjinkim
Copy link
Author

Helped me out so much! Thanks for this

Happy that it helped you @chintani @jmmichaud @Isaamarod @yogenderPalChandra !

@ramazeinin
Copy link

I get error when I run:
pg_ctl -D mylocal_db -l logfile start
the error:
pg_ctl: could not start server
Examine the log output.

@gwangjinkim
Copy link
Author

@IceBear0149 Then probably you should examine the log output ;)

@A-C-N
Copy link

A-C-N commented Oct 17, 2022

This saved my time! thank you

@AlexandreFigueira
Copy link

It helped a lot!

@dongdongju96
Copy link

Thank you to your help :)
If you don't mind, Let me know reference or document about code?

@gwangjinkim
Copy link
Author

Thank you to your help :) If you don't mind, Let me know reference or document about code?

Which reference or document you mean? Except the normal instructions for global postgresql - how to do it in the conda environment, I had to found out by trial and error and guessing.

@dongdongju96
Copy link

dongdongju96 commented Mar 11, 2023

Thank you to your help :) If you don't mind, Let me know reference or document about code?

Which reference or document you mean? Except the normal instructions for global postgresql - how to do it in the conda environment, I had to found out by trial and error and guessing.

Oh, I understood. Thanks.

@Pcr-dev
Copy link

Pcr-dev commented Feb 17, 2026

Thank you for the information ❤️

@gwangjinkim
Copy link
Author

@Pcr-dev Welcome! Happy that it helped you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment