PostgreSQL for Rails on Apple OS X
This could be a short tutorial to follow along, how to setup PostgreSQL for your development environment. Basically it’s going to be a sharing my experience about, when I tried to understand more of it from my Rails Meetup Study Group.
I’ve done it on Apple machine, so started with following (Just in case, starting this with brew updating)
brew update && brew upgrade brew install postgres
Of course it should be done after you install homebrew package manager first if not done yet, and for me I simply followed their easiest way on brew.sh page.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Check if following
psql command working. (you will see your username of the machine in place of
your_username, and more lines omitted here.)
$ psql --help psql is the PostgreSQL interactive terminal. General options: -d, --dbname=DBNAME database name to connect to (default: "your_username") Connection options: -h, --host=HOSTNAME database server host or socket directory (default: "local socket") -p, --port=PORT database server port (default: "5432") -U, --username=USERNAME database user name (default: "your_username") -w, --no-password never prompt for password -W, --password force password prompt (should happen automatically)
That means for example, when you type simply this command:
without any options like
-d some_dbname or
--dbname=some_dbname, then it considers your command as something like following:
psql -U your_username -d your_username
However, because it said “The first database is always created by the initdb command when the data storage area is initialized. This database is called postgres.” on the documentation, so I did
psql -d postgres
…and got an error. This error was given because I didn’t do create the first database by the initdb command. I followed another page of documentation: “There is no default, although locations such as
/var/lib/pgsql/data are popular.”
$ initdb -D /usr/local/pgsql/data
$ psql -d postgres \psql (9.4.4) Type "help" for help. postgres=#
Now this should be working (exit with
\q by the way. for all other backslash commands, check here) while database running.
Always don’t forget the database must be up and running to use it. For starting postgres, generally you do
postgres -D /usr/local/pgsql/data
But again, I’ve done it on Apple machine, I consider you may
brew services start after you install homebrew services because it’s very simple.
brew tap homebrew/services
brew services start postgresql brew services list
Now all done, and let’s start with
rails new command with
rails new app_name -d postgresql
If it’s already in the middle of working with
sqlite3 gem in your Gemfile, you may change it from something like this
group :development, :test do gem 'sqlite3' end
group :development, :test do gem 'pg' end
config/database.yml file from something like
default: &default adapter: sqlite3 pool: 5 timeout: 5000 development: <<: *default database: db/development.sqlite3
into something like
default: &default adapter: postgresql encoding: unicode pool: 5 development: <<: *default database: app_name_development
or you may config username and password to access the database:
development: adapter: postgresql encoding: unicode database: app_name_development pool: 5 username: your_username password:
By the way your your_username is not necessary here. Because we mentioned psql command has –username=your_username (Mac user name) by default. (You can always set the
password up differently, but I don’t think it necessary in development.)
app_name_test should be created by
And then, go back to normal Rails development and test, maybe with
and so on. They say you should use
pg gem because it’s the same one with default database of heroku. Maybe that might be why you are going to do this.