2

I need to use postgresql with ruby on rails.

I have these directories

/Library/PostgreSQL

and

/usr/local/Cellar/postgresql/usr:local:Cellar:postgresql:9.2.2

This 2nd directory name is confusing. In finder, when I'm in the directory

/usr/local/Cellar/postgresql,
usr:local:Cellar:postgresql:9.2.2 

is actually one directory with forward slashes in the name. usr/local/Cellar/postgresql. It's in terminal where I see the :'s

"brew info postgres" returns the following:

postgresql: stable 9.2.2
http://www.postgresql.org/
Depends on: readline, ossp-uuid
/usr/local/Cellar/postgresql/usr:local:Cellar:postgresql:9.2.2 (2819 files, 39M)
https://github.com/mxcl/homebrew/commits/master/Library/Formula/postgresql.rb

So do I have postgres installed properly? Do I have 2 versions? One in Library/PostgreSQL and one in /usr/local/Cellar (via HomeBrew)?

Regarding trying to use initdb to create my first postgres database:

I have ohmyzsh installed and I am trying to run the initdb command but getting command not found. Which path should I put in my path statement so that it can find the command?

/usr/local/var/postgres/server.log did not exist, so I created it.

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start says server is starting and when I do ps auxwww | grep postgres

I get:

postgres   260   0.0  0.1  2522180   8084   ??  SNs  Tue07PM   0:58.79
/System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdworker MDSImporterWorker com.apple.Spotlight.ImporterWorker.504

so I think that means it is running, right?

Well I found initdb script file in both Library and brew locations. I navigated to the brew directory containing initdb and tried to run the initdb command and got zsh: command not found.

I then navigated to the Library containing initdb, tried to run it, and got the same results.

Isn't the current directory part of the path because I'm IN IT?

Summary of my questions:

  • Which postgres do i have installed? Is it installed twice and should I uninstall a version? If so, how?
  • What directory needs to be in my path to run initdb and do I really need to run it?
  • Should the postgres server start when I start my computer? I heard it should. How do I set that up?
3
  • 1
    On unix/osx the current directory is often NOT in the path as this is considered a security risk. To run something from the current directory you can explicitly specify the current directory by doing ./initdb (a single dot is the current directory)
    – Eelke
    Commented Mar 31, 2013 at 7:29
  • UPDATE: I put /Library/PostgreSQL/9.2/bin in my path in my .zshrc file, and psql now does something when I try to run it. Now I have an error about unix socket directory, which I'm going to chase down. I do still have the questions above.
    – Darby
    Commented Mar 31, 2013 at 8:08
  • Error's about the UNIX domain socket are because either postgresql is not running or the version which is running has put it in another location then the psql version. You can either try to find a version on psql that uses the same path or use TCP/IP by specifying -h 127.0.0.1
    – Eelke
    Commented Mar 31, 2013 at 12:49

1 Answer 1

3

Based on your situation, it would make sense I think to disable whatever PostgreSQL you have and start over with a fresh install. We are dealing with completely different applications that have nothing to do with eachother. In reality, PostgreSQL is only needed to serve up data. It makes sense to me, to simplify your life by installing PostgreSQL seperately and treating it as a totally independant application.

The simplest way to go about this, would be to take the following steps.

  1. Determine if PostgreSQL is currently running A. If it is, locate it and disable it B. If it isn't move onto step 2
  2. Download PostgreSQL 9.2 from PostgreSQL's website
  3. Install dependancies
  4. Configure, compile and install postgresql
  5. Initalize your database and start postgres
  6. What's next

1. Determine if PostgreSQL is currently running

From the shell, type the following

[root@someserver ~]# netstat -tlpn | grep postmaster
tcp        0      0 0.0.0.0:5432                0.0.0.0:*                   LISTEN      1119/postmaster     
tcp        0      0 :::5432                     :::*                        LISTEN      1119/postmaster    

Postmaster on my machine is the PostgreSQL service. It is listening on port 5432 and has a process id of 1119.

Where is it?

Now that we have the process ID we can locate the files being used. lsof is handy because it will show you all the files and sockets a given process is using.

[root@someserver ~]# lsof -p 1119
COMMAND    PID     USER   FD   TYPE             DEVICE SIZE/OFF   NODE NAME
postmaste 1119 postgres  cwd    DIR              253,3     4096 131093 /var/lib/pgsql/9.1/data
postmaste 1119 postgres  rtd    DIR              253,0     4096      2 /
postmaste 1119 postgres  txt    REG              253,4  5205008  27145 /usr/pgsql-9.1/bin/postgres

On my server, it is using /usr/pgsql-9.1/bin/postgres to run the service and the /var/lib/pgsql/9.1/data is the data directory.

From this point you know where its running and what directories its using.

On your server, it looks like its installed in /usr/local/Cellar/postgresql. If you have to start it manually, then you can just leave it alone. When you install PostgreSQL from source, it will install in /usr/local/pgsql by default.

It is only important to be sure that the postgresql currently installed on your system is not using port 5432. This is because two services can not use the same port. If it is, you will need to kill the process and keep it from starting again. Based the contents of your question, it looks like that won't be a problem.

2. Download postgresql

From the shell

[root@someserver ~]# cd ~ 
[root@someserver ~]# wget http://ftp.postgresql.org/pub/source/v9.2.2/postgresql-9.2.2.tar.gz
[root@someserver ~]# tar -zxvf postgresql-9.2.2.tar.gz

3. Install dependencies

I didn't notice you mentionning what flavor of Linux you were running. You can identify your OS by running cat /etc/issue.

PostgreSQL depends on several libraries.

On CentOS, I would run this to install the libraries I needed

[root@someserver ~]# yum install readline-devel zlib-devel make gcc g++

On Debian, you would use apt-get to install and apt-cache search to locate. I included build-essential because you'll need it to compile postgres.

Something like apt-get install libreadline6 libreadline6-dev build-essential

Not sure about OSX though

4. Configure, Compile and Install PostgreSQL

[root@someserver ~]# 
cd postgresql-9.2.2
The --bindir option will put the the postgresql binaries like pg_ctl, initdb, createdb all in the /usr/bin directory. This will be handy for you since you mentionned having trouble finding them. 
./configure --bindir=/usr/bin
make
make install

You can check to see if the binaries are available

[root@someserver ~]# psql -V
psql (PostgreSQL) 9.2.2

Once this is done, normally you would need to add the postgres user, but he's already on your system You do however need to create a data directory and give postgres read/write access to it

[root@someserver ~]# mkdir -p /var/lib/pgsql/data
[root@someserver ~]# chown postgres:postgres -R /var/lib/pgsql/data
[root@someserver ~]# su - postgres

PostgreSQL is installed You have created a data directory Now you can initalize your database

5. Initalize and start postgres

initdb -D /var/lib/pgsql/data

This will the cluster and your configuration files such as postgresql.conf, pg_hba.conf which you will need to modify later based on the users that need access, their ip addresses and what ip address and port postgresql will listen on

Now you can start PostgreSQL with the following command :

/usr/local/pgsql/bin/postmaster -D /var/lib/pgsql/data > /var/lib/pgsql/logfile 2>&1 &

When you run netstat -tlpn again, you should see

[root@someserver ~]# netstat -tlpn | grep postmaster
tcp        0      0 127.0.0.1:5432                127.0.0.1:*                   LISTEN      8721/postmaster     

It is only listening to local connections for now, because by defaut that's what postgresql.conf is configured to do.

6. What's next

From here, you would need to configure postgresql.conf and pg_hba.conf appropriately. pg_hba.conf defines who can connect, from where and to what. postgresql.conf is used to configure the entire service, what ip address or addresses it listens on, what port number, maintenance cyles, etc. For your purposes, you will probabaly only be interested in changing this variable

listen_addresses = '*'

You may also want to create an init script in the /etc/init.d directory, so it will start automatically. How you go about that also depends on what flavor of Linux you are running.

If you run into any problems let me know. Since you are probably not using the exact same system, you may run into problems I haven't discussed here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.