Skip to content

Instantly share code, notes, and snippets.

View harshalbhakta's full-sized avatar

Harshal Bhakta harshalbhakta

View GitHub Profile
@harshalbhakta
harshalbhakta / steps.md
Created July 29, 2014 09:40
Setup Postgresql with hstore on Ubuntu Server 12.04 LTS.
$ add-apt-repository ppa:pitti/postgresql
$ apt-get update
$ apt-get install postgresql libpq-dev
$ sudo -u postgres psql

postgres=# \password
Enter new password: 
@harshalbhakta
harshalbhakta / single-line-rails-console-commands.rb
Last active August 29, 2015 14:03
Single line rails console commands.
$ User.all.each {|user| user.update_attributes(:active => true) }
$ (1..100).each { InfoPack.find(246).sections.each { |s| s.sub_items.each { |si| si.update_attributes(:active => true) } } }
@harshalbhakta
harshalbhakta / sites-enabled_app1
Last active August 29, 2015 14:02
Maintenance config for nginx.
upstream unicorn {
server unix:/tmp/unicorn.app1-web.sock fail_timeout=0;
}
server {
listen 80 default deferred;
# server_name example.com;
root /home/deployer/apps/app1/current/public;
# Maintenance state start
@harshalbhakta
harshalbhakta / console
Created June 23, 2014 06:29
Connect to remote Postgresql db from PgAdmin
# Postgresql database is running on server 192.168.0.1:5432 for user deployer
#
# Crearte SSH tunnel for pgadmin to connect to remote server.
# localhost@5555 => 192.168.0.1@5432
# Connect using localhost & 5555 in PgAdmin
$ ssh -fNg -L 5555:localhost:5432 deployer@192.168.0.1
@harshalbhakta
harshalbhakta / gist:178563e42c127b28f374
Created June 18, 2014 13:23
Delete open Postgresql connections
-- http://stackoverflow.com/a/5408501
-- PostgreSQL 9.1 and below:
SELECT pg_terminate_backend(pg_stat_activity.procpid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'TARGET_DB'
AND procpid <> pg_backend_pid();
-- PostgreSQL 9.2 and above:

Here's how to install PostgreSQL and have it run automatically at startup, on an Ubuntu 10.04 virtual machine using Vagrant. This took me a while to figure out:

Add the default lucid32 base box to your vagrant, if you haven't already:

host> vagrant box add lucid32 http://files.vagrantup.com/lucid32.box 

Now make a new lucid32 virtual machine and install postgresql on it:

@harshalbhakta
harshalbhakta / rails_installation_RDoc_Warning
Created November 7, 2013 05:56
RDoc warning while installing Rails.
$ gem install rails
Fetching: i18n-0.6.5.gem (100%)
Successfully installed i18n-0.6.5
Fetching: multi_json-1.7.9.gem (100%)
Successfully installed multi_json-1.7.9
Fetching: tzinfo-0.3.37.gem (100%)
Successfully installed tzinfo-0.3.37
Fetching: atomic-1.1.13.gem (100%)
Building native extensions. This could take a while...
Successfully installed atomic-1.1.13
curl http://localhost:3000/api/v1/Action -H 'Authorization: Token token="api_key_goes_here"'
# Authenticate:
curl -H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-X POST http://localhost:3000/users/sign_in \
-d "{'user' : { 'email' : 'test@example.com', 'password' : 'password'}}" \
-c cookie
# Show:
@harshalbhakta
harshalbhakta / angular_factory_resource.js.coffee
Created September 16, 2013 07:13
Angular resource with additional methods.
app.factory "User", ["$http","$resource",($http,$resource) ->
User = $resource("/roles/:id", {id: "@id"}, {update: {method: "PUT"}})
# Initialize the data using a static method
User.init = (data) ->
angular.extend this, data
# a static method to retrieve User by ID
User.get_it = (id) ->