Setting up PostgreSQL for Ruby on Rails on MacOS


Setting up PostgreSQL for Ruby on Rails development on Mac OS X

NB: We will use HOMEBREW, To get Homebrew on your machine: https://github.com/mxcl/homebrew

Installing PgSQL

$ brew install postgres 

Creating a database

$ initdb /usr/local/var/postgres

=> you may have an error relative to segment memory on Leopard:
creating template1 database in ../../DB/base/1 ... FATAL:  could not create shared memory segment: Cannot allocate memory
DETAIL:  Failed system call was shmget(key=1, size=2138112, 03600).

=> to fix this problem:
sudo sysctl -w kern.sysv.shmall=65536
sudo sysctl -w kern.sysv.shmmax=16777216

if all is OK, you will get something like:
creating directory /Users/walid/DB_PgSQL ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 8000kB
creating configuration files ... okls
creating template1 database in /Users/walid/DB_PgSQL/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the -A option the
next time you run initdb
Success. You can now start the database server using:
    ./postgres -D /Users/walid/DB_PgSQL
or
    ./pg_ctl -D /Users/walid/DB_PgSQL -l logfile start


/!\ to remove the database use: 

$ dropdb -U username -i database


Starting the database

$ ./pg_ctl -D /Users/walid/DB_PgSQL -l logfile start

Create User (role)

$ sudo -u postgres createuser -s SoulRebel

to create a user with password: 
$ sudo -u postgres createuser -s -P -e SoulRebel

Create database

$ sudo -u postgres createdb -O SoulRebel -Eutf8 MyDB_dev
$ sudo -u postgres createdb -O SoulRebel -Eutf8 MyDB_test

How the Recreate the database ?

two ways to do this:
- Reset your database and reload your current schema with all:


rake db:reset


$ rake db:migrate
- Destroy your db and then create it and then migrate your current schema:


$ rake db:drop
$ rake db:create
$ rake db:migrate

No comments:

Post a Comment