Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gayathry2612/8f62c317ffddb6e2be51517cad729d17 to your computer and use it in GitHub Desktop.
Save gayathry2612/8f62c317ffddb6e2be51517cad729d17 to your computer and use it in GitHub Desktop.
How to install and run postgresql in conda
# Step by Step instructions to install PostGres through Conda (DJANGO TUTORIAL)
This gist I write, because I couldn't find step by step instructions
how to install and start postgresql locally and not globally in the
operating system (which would require sudo).
I hope, this will help especially people new to postgresql!
Assumptions :
You have already installed conda or miniconda and can call conda from the terminal.
If you havent done that please check out anaconda or miniconda installation on your OS.
Supported OS :
MacOS, Windows, Linux
####################################
# create conda environment
####################################
$conda create --name myenv
# enter the environment
$ conda activate myenv
####################################
# install postgresql via conda
####################################
$conda install postgresql
If that fails, check for
$conda install -y -c conda-forge postgresql
#####################################
# You have installed postgres. Now create a "base" database locally.
If this base db doesn't exist, it will raise a configuration file error.
This command will create a configuration file to run the postgres locally.
####################################
$initdb -D mylocal_db
##############################
# now start the server modus/instance of postgres.
You might encounter a server not active error otherwise.
##############################
$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 django_user
# asks for name and password
####################################
# using this django user, create inner database inside the base database
####################################
$createdb --owner=django_user myinner_db
####################################
# in this point, if you run some program,
# you connect you program with this inner database
# e.g. Django
####################################
# in django (Python) e.g. you do
$nano <mysite>/settings.py # or instead of nano your favorite editor!
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
# Edit the preset name. eg - BASE URL / something to 'myinner_db'
'NAME': 'myinner_db',
'USER': 'django_user',
'PASSWORD': '<django_user password>',
'HOST': 'localhost',
'PORT': '', # leave it as blank, or put the same port as the db server.
}
}
# and it is available for django
# so that you can do
##############################
# do with the connected program 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 database, you also need to create a superuser
python manage.py createsuperuser --username name
################################
# stop running postgres instance under ubuntu (works for macOS)
################################
# monitor whether a postgres instance/server is running or not
ps aux | grep postgres
# if none is running, you will se one line - 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.
# 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):
kill 30550
####################################
# run postgres as non-server in 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 below (under 'stop running postgres instance under ubuntu')!
kill <process ID> # to stop postgres
Have fun with your completely locally running - more safe - postgresql!!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment