Showing posts with label database. Show all posts
Showing posts with label database. Show all posts

Thursday, September 18, 2014

Postgres As Docker

CONTAINER_NAME="postgres_server"
CONTAINER_DATA_DIR="/var/lib/postgresql/data"
HOST_PORT="55432"
POSTGRES_DOCKER_DIR=`pwd`
HOST_DATA_DIR="${POSTGRES_DOCKER_DIR}/data-dir/"

if [ ! -d "${HOST_DATA_DIR}" ]
then
    mkdir -p "${HOST_DATA_DIR}"
fi

sudo docker run --rm --name ${CONTAINER_NAME} \
            -v ${HOST_DATA_DIR}:${CONTAINER_DATA_DIR} \
            -p ${HOST_PORT}:5432 \
            -it postgres $1

Wednesday, May 15, 2013

Postgres Configuration on RedHat

This will install Postgresql on a RedHat 6.4 or CentOS 6.4 system and will allow password-protected remote login access. 

# Open the firewall to allow remote postgres connections
sudo /sbin/iptables -A INPUT -p tcp --dport 5432 -j ACCEPT

sudo /sbin/service iptables save
sudo /sbin/service iptables restart

# Install packages through yum
sudo yum install vim-enhanced
sudo yum install postgresql-server

# Initialize a new database directory
sudo service postgresql initdb 

# Start the database server
sudo service postgresql start 

# Add postgres to the default runlevel 
sudo chkconfig postgresql on
sudo chkconfig postgresql --list
sudo chmod 755 /home/idcuser/

# Edit the postgres.conf to allow connections from all ips
sudo vim /var/lib/pgsql/data/postgres.conf
listen_addresses="*"

# Edit the pg_hba.conf file to allow password auth for any ip
sudo vim /var/lib/pgsql/data/pg_hba.conf
host all all 0.0.0.0/0 md5

# Restart postgres
sudo /etc/init.d/postgres restart

# Configure a postgresql superuser and new schema. 
sudo -u postgres psql
psql> create role idcuser superuser createdb createrole inherit login;
psql> alter user idcuser with password 'password';
psql> create database sample;
psql> \c sample
psql> create schema sample;
psql> alter user idcuser set search_path to sample,public;

Wednesday, February 27, 2013

DB2 command reference


Invoke interpreter:
shell> db2

List available databases:
db2_shell> list database directory

List DB2 shell history:
db2_shell> history
# or
db2_shell> h

Edit and Execute command from DB2 shell history:
# command_num is from the above history command
db2_shell> edit command_num
# or
db2_shell> e command_num

Connect to database from within the db2 interpreter (CLP):
db2_shell> connect to databasename

Connect to database from UNIX shell:
# lasts for duration of current shell session.
db2 connect to database_name

Run SQL command from UNIX shell:
# first connect to database via UNIX shell (above)
# then run the following
db2 -vf scriptname.sql

List tables in database:
db2_shell> list tables

List indexes on a table:
SELECT indschema,INDNAME from syscat.indexes where tabname = 'MYTABLE' and tabschema = 'MYSCHEMA'

Analyze table:
runstats on table schema.tablename

List connections to database:
unix> db2 list applications
# or
db2_shell> list applications
# for detail information
unix> db2 list applications show detail

Get current user:
SELECT CURRENT USER FROM SYSIBM.SYSDUMMY1

Disconnect all connections:
force applications all
#or 
db2 terminate # this might work


Drop database:
drop database databasename

Create database:
create database databasename

Create the explain tables:
connect to databasename
db2 -vtf /opt/ibm/db2/V9.7/misc/EXPLAIN.DDL

Stop DB2:
db2stop

Start DB2: 
db2start

Allow TCP connections (probably unsafe):
update database manager configuration using svcename 3700
get database manager configuration    # verify change
db2set DB2COMM=tcpip
db2stop
db2start
sudo /sbin/iptables -A INPUT -p tcp --dport 7300 -j ACCEPT
sudo /sbin/service iptables save
sudo /sbin/service iptables restart

Update config params:
update db cfg for database_name using logfilsiz 8000
 # or
update database configuration for database_name using logprimary 9 logfilsiz 8000
force applications all

Sources:
http://www.tek-tips.com/viewthread.cfm?qid=128876

Tuesday, October 19, 2010

Django List Installed Tables and Models

Get Database connection
# Django 1.2
from django.db import connections
connection = connections['default']

# Django 1.1.1<=
from django.db import connection

# Get Database tables
tables = connection.introspection.table_names()

# Get Database models
modelclasses = connection.introspection.installed_models(tables)

# Get Django model name and app name
for model in list(modelclasses):
    print model._meta.app_label, model._meta.module_name

# A better way to get Django models
from django.db.models.loading import get_models, get_app
get_app('app_label')  # returns an app class given a label
get_models()  # all models 
get_models(application)  # models belonging to app class

# Get a model from a string
from django.db import models
models.get_model(*"myapp.mymodel".split('.')) # returns model class

# Get all Python modules
import sys
sys.modules

http://www.slideshare.net/ubernostrum/django-in-depth?src=related_normal&rel=3017822


Wednesday, February 03, 2010

Oracle Login Context Information

Find the Database instance you are logged into
SELECT sys_context('USERENV', 'DB_NAME') FROM dual;
SELECT sys_context('USERENV', 'INSTANCE_NAME') FROM dual;

Find the operating system user hosting the Oracle session
SELECT sys_context('USERENV', 'OS_USER') FROM dual;

Find the Oracle user currently logged into
SELECT sys_context('USERENV', 'SESSION_USER') FROM dual;

http://www.psoug.org/reference/sys_context.html

Wednesday, October 07, 2009

Django South Migrations

Install Django South
- Place the untared 'south' folder into your Django applicaion
- Modify the project's settings.py and add 'south' to the INSTALLED_APPS list
- For new Django projects:
---1) initialize any Django apps or models to monitor with South.
----a) See Initializing Applications below
---2) run ./manage.py syncdb
---3) run ./manage.py migrate

To view help on startmigration:  # South 7.x+ uses the schemamigration command instead
./manage.py startmigration
Usage: ./manage.py startmigration appname migrationname [--initial] [--auto]
[--model ModelName] [--add-field ModelName.field_name] [--freeze] [--stdout]

Initializing Applications - needed whenever you want to track more Django apps
# this creates a 'migrations' folder in your Django app directory
./manage startmigration app_name comment --initial  # updates for South 7.x+
./manage schemamigration app_name comment --initial # Init's all models in application
./manage startmigration app_name --initial # updates for South 7.x+
./manage schemamigration app_name --initial # comment is optional here

Adding South with no data and no existing models.py objects:
# 1) syncdb as normal
python manage.py syncdb
# 2) Start an application if needed
python manage.py startapp appname
# 3) create a migration file with a blueprint to create tables
python manage.py startmigration appname --initial 
 # updates for South 7.x+
python manage.py schemamigration appname --initial
# 4) Apply the blueprint and create the tables
python manage.py migrate appname
# 5) Repeat steps 3 and 4 for each additional app if needed

Adding south with no data and existing models.py objects:
# 1) syncdb as normal if needed
python manage.py syncdb
# 2) create a migration file with a blueprint to create tables
python manage.py startmigration appname --initial # updates for South 7.x+
python manage.py schemamigration appname --initial
# 3) Apply the blueprint but it doesn't actually create the tables
python manage.py migrate appname --fake
# 4) Repeat steps 2 and 3 for each additional app if needed

Applying changes after making changes to a model (repeat for each app):
# Creates a migration 'blueprint' file and guesses what should take place
python manage.py startmigration appname migrationname --auto # updates for South 7.x+
python manage.py schemamigration appname migrationname --auto
# OR create the blueprint file but it will be empty
python manage.py startmigration appname migrationname  # updates for South 7.x+
python manage.py schemamigration appname migrationname

# make any modifications needed
# migrate the tables in the app
python manage.py migrate appname
# OR migrate all tables in all apps
python manage.py migrate

Adding a new model to an app already managed with south:
./manage startmigration app_name comment --model m1 --model m2 # updates for South 7.x+
./manage schemamigration app_name comment --model m1 --model m2 # inits specific models

List available migrations
python manage.py migrate --list

http://south.aeracode.org/wiki/ConvertingAnApp