PostgreSQL useful commands

sudo -u postgres psql

list all databases

\l

connect to database xxx

\c xxx

list all tables in the current database

\dt

list all tables and indexes in current database

\d

list column yyy detail

\d yyy

drop table xxx and yyy in current database

drop table xxx, yyy;

drop xxx database

DROP DATABASE "xxx";

show postgres version

select version();

show current database

SELECT current_database();

connection info

\conninfo

exit psql

\q

rename database

ALTER DATABASE oldame RENAME TO newname;

clone database

CREATE DATABASE newdb WITH TEMPLATE originaldb;

Existing user as admin in Django

Enter Django shell

python manage.py shell

edit user settings

from django.contrib.auth.models import User
user = User.objects.get(username="myname")
user.is_staff = True
user.is_admin = True
user.is_superuser = True
user.save()

Git useful commands

Repositories

List remote repositories

git remote -v

Add new remote repository

git remote add <REMOTE_REPO_NAME> https://github.com/<OWNER>/<REPOSITORY>.git

In case of fork, it’s useful to add the original repo as a remote repo named ‘upstream’

git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git

Sync fork repo with original repo

First, fetch original repo

git fetch upstream

Then, switch to fork/branch to sync with original branch (eg. master)

git checkout master

Merge original/branch with fork/branch

git merge upstream/master

Finally, push synced fork/branch in remote fork/repo (eg. master)

git push origin master

Branches

List Branches

only local branches

git branch

both remote-tracking and local branches

git branch -a

Create new branch and switch to new Branch

git checkout -b <branch_name>

Clone a specific Branch

Create local Branch linked to remote Branch

Switch Branch

git checkout <branch_name>

Remove Branch

Remove local branch

git branch -d <branch_name>

Remove remote branch once removed local branch

git push origin :<branch_name>

Push changes to Branch

git push origin <branch_name>

Merge Branches

switch to destination branch
than commit selected branch into current branch
--no-ff option generate a merge commit with a message
git merge --no-ff <branch_name>

Tags

List Tags

git tag

Create Tag

Push Tag

Push all tags

git push --tags

Push a single tag

git push origin <tag_name>

Undo

Discharge changes on file or restore deleted file

Undo a commit

TIPS

After fetching, remove any remote-tracking branches which no longer exist on the remote.

git fetch --prune

Create a local branch named xxx, tracking the remote branch origin/xxx

git checkout --track origin/xxx

Manax on DreamHost

  • create a new user in your dreamhost account

  • create a new domain for your project

  • create a MySQL database for your project

  • login the just created account

  • install latest Python release

  • enter domain folder (eg. example.com)

    cd example.com
    
  • Create a virtualenv

    pyvenv-3.5 venv
    source venv/bin/activate
    
  • clone your manax project (eg. my_project)

    git clone [your project git]
    source venv/bin/activate
    
  • create a file passenger_wsgi.py in the root of your website directory (eg. example.com). Replace ‘my_project’ with your project name.

    import sys, os
    cwd = os.getcwd()
    sys.path.append(cwd)
    sys.path.append(cwd + '/my_project')
    
    INTERP = os.path.abspath(cwd + '/venv/bin/python')
    
    if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)
    
    sys.path.insert(0,cwd+'/venv/bin')
    sys.path.insert(0,cwd+'/venv/lib/python3.5/site-packages')
    
    os.environ['DJANGO_SETTINGS_MODULE'] = "my_project.settings"
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
    
  • Create the restart file to start your passenger site

    mkdir tmp
    touch tmp/restart.txt
    
  • Create folder for static files

    mkdir public/static
    
  • edit env.dreamhost

  • source env.dreamhost

    source env.dreamhost
    
  • enter project folder

    cd my_project
    
  • edit config/settings/dreamhost.py

  • install requirements

    pip install -r requirements/dreamhost.txt
    
  • Run the collectstatic command to set up the static items

    python manage.py collectstatic
    
  • perform makemigrations for every app

  • create database

    python manage.py migrate
    
  • Start manax project via

    python passenger_wsgi.py
    

To restart manax project, for example after some modifications

touch tmp/restart

Manax howto

Move to your development folder (eg: $HOME/workspace)

cd $HOME/workspace

create and activate a virtualenv

pyvenv-3.5 venv
source venv/bin/activate

upgrade pip and install cookiecutter

pip3 install --upgrade pip
pip install cookiecutter

start new django project via manax

cookiecutter https://github.com/manazag/manax

You’ll be prompted for some values. Provide them, then a Django project will be created for you. Answer the prompts with your own desired options. For example:

Cloning into 'manax'...
remote: Counting objects: 550, done.
remote: Compressing objects: 100% (310/310), done.
remote: Total 550 (delta 283), reused 479 (delta 222)
Receiving objects: 100% (550/550), 127.66 KiB | 58 KiB/s, done.
Resolving deltas: 100% (283/283), done.
project_name [project_name]: Reddit Clone
project_slug [Reddit_Clone]: reddit
author_name [Your Name]: Daniel Roy Greenfeld
email [Your email]: pydanny@gmail.com
description [A short description of the project.]: A reddit clone.
domain_name [example.com]: myreddit.com
version [0.1.0]: 0.0.1
timezone [UTC]: America/Los_Angeles
now [2016/03/01]: 2016/03/05
year [2016]:
Select db_backend:
1 - sqlite
2 - postgresql
Choose from 1, 2 [1]:
use_whitenoise [y]: n
use_celery [n]: y
use_mailhog [n]: n
use_sentry [n]: y
use_newrelic [n]: y
use_opbeat [n]: y
use_pycharm [n]: y
windows [n]: n
use_python2 [n]: n
use_docker [y]: y
use_heroku [n]: n
use_grunt [n]: y
use_angular [n]: n
Select open_source_license:
1 - MIT
2 - BSD
3 - Not open source
Choose from 1, 2, 3 [1]: 1

move into [project_slug] folder:

cd [project_slug]

if this is first time you use manax, run this script

sudo ./utility/install_os_dependencies.sh install

if you want a virtualenv inside [project_slug] folder

deactivate
pyvenv-3.5 venv
source venv/bin/activate

install requirements

pip install -r requirements/local.txt

now, only a database is between you and your project running

export DATABASE_URL="sqlite:///$PWD/db.sqlite"
python manage.py migrate
python manage.py runserver

deploy Django on DreamHost

First, enter domain folder (eg. example.com)

cd example.com

Create a virtualenv and install Django (eg. using python 3.5).

pyvenv-3.5 venv
source venv/bin/activate
pip install django

Create (or install from a repo) your django project (eg. my_project).

Create a file passenger_wsgi.py in the root of your website directory (eg. example.com). Replace ‘my_project’ with your project name.

import sys, os
cwd = os.getcwd()
sys.path.append(cwd)
sys.path.append(cwd + '/my_project')

INTERP = os.path.abspath(cwd + '/venv/bin/python')

if sys.executable != INTERP: os.execl(INTERP, INTERP, *sys.argv)

sys.path.insert(0,cwd+'/venv/bin')
#sys.path.insert(0,cwd+'/venv/lib/python3.5/site-packages/django')
sys.path.insert(0,cwd+'/venv/lib/python3.5/site-packages')

os.environ['DJANGO_SETTINGS_MODULE'] = "my_project.settings"
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Create the restart file to start your passenger site

mkdir tmp
touch tmp/restart.txt

Create folder for static files

mkdir public/static

Set up Django’s static file settings in order to correctly serve images, CSS, and JavaScript as you will need this for the admin interface to work. Edit my_project/settings.py to configure STATIC_ROOT

STATIC_ROOT = os.path.dirname(BASE_DIR) + '/public/static/'

Run the collectstatic command to set up the static items for the admin interface

cd $HOME/example.com/my_project
python manage.py collectstatic

Start django project via

python passenger_wsgi.py

To restart django project, for example after some modifications

touch $HOME/example.com/tmp/restart

MySQL backend

Activate virtualenv and install mysqlclient driver

pip install mysqlclient

Create a MysQL database in DreamHost panel.

Edit my_project/settings.py to configure database

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '<db_name>',
        'USER': '<user>',
        'PASSWORD': '<password>',
        'HOST': '<mysql.example.com>',
        'PORT': '',
    }
}

Install Python on DreamHost

First, ssh into your server and create a 'python' folder in home directory

cd ~
mkdir python
cd python

Download and extract python source (eg. python 3.5.2)

Download and extract python source (eg. python 3.5.2)

wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tar.xz
tar xvfJ Python-3.5.2.tar.xz
cd Python-3.5.2

Compile the source and install it in a properly named directory (eg ‘Python35’)

./configure --prefix=$HOME/Python35
make
make install

Add the installation to the PATH variable.

echo '# Python3 PATH' >> ~/.bash_profile
echo 'export PATH=$HOME/Python35/bin:$PATH' >> ~/.bash_profile
source ~/.bash_profile

Done.

upgrade pip (optional)

pip3 install --upgrade pip