PostgreSQL for Mac OS X
Pages: 1, 2
Setting Up a Database
In order to simplify things for the purpose of this article, we're going to create the database as your own user, which should be set up as an administrator. However, for production systems, it is recommended that you create the database as a dedicated PostgreSQL user.
Initialize the database cluster by typing the following:
initdb -D /Users/Shared/PostgreSQL/data
PostgreSQL will begin this task by displaying feedback similar to what you see below.
The files belonging to this database system will be owned by user "steve".
This user must also own the server process.
It will then display the feedback below to indicate that it is finished:
Success. You can now start the database server using:
/Users/Shared/PostgreSQL/bin/postmaster -D /Users/Shared/PostgreSQL/data
or
/Users/Shared/PostgreSQL/bin/pg_ctl -D /Users/Shared/PostgreSQL/data -l logfile start
Which is just what we want to do. The first command will start the database server in the foreground. The second command uses a shell script that automates the commands needed to start the database server in the background. Start the database with the second command.
Let's create our first database. Actually, PostgreSQL's first database (template1) was created when we initialized the database cluster. However, we're now going to create the first real database.
createdb foo
Congratulations, you just created your first database! Its name is "foo". That wasn't so hard.
Accessing the Database
Let's try accessing the database we just created. There are a couple of SQL tools for the Mac I'll touch on: ViennaSQL, and SQL4X Manager J. I'll walk you through setting up ViennaSQL and connecting to foo with it. But, I've also got a screenshot of the key connection setup screen if you wish to use SQL4X Manager J instead.
First, download the Java Archive (JAR) file for ViennaSQL. Then download the JDBC driver for PostgreSQL. I renamed pgjdbc2.jar to postgresql.jar and placed it in /Library/Java/Extensions. Now add this JAR and the current directory to the classpath environment variable.
setenv CLASSPATH ${CLASSPATH}:/Library/Java/Extensions/postgresql.jar:.
You will need to edit your postgresql.conf file in order to activate TCP/IP sockets for PostgreSQL. The file is located in /Users/Shared/PostgreSQL/data/. You will see the following line near the top of the file under the connection parameters section:
#tcpip_socket = false
Uncomment the line by removing the pound symbol (#) and replace "false" with "true." Open /Users/Shared/PostgreSQL/data/pg_hba.conf in a text editor. Scroll to the bottom of the host-based access file and enter the following to allow access to all databases from any computer on the local network:
host all 192.168.0.0 255.255.0.0 trust
Restart the postmaster by issuing the following command:
/Users/Shared/PostgreSQL/bin/pg_ctl _
-D /Users/Shared/PostgreSQL/data _
-l logfile -o -i restart
Be sure to specify the -o and -i arguments to turn on IP sockets.
|
Related Reading Learning Unix for Mac OS X |
OK, now start up ViennaSQL via its JAR file. Under File in the menu bar select Configure. This will bring up the ViennaSQL options window. Click on the Connection tab and create a new connection. Fill in the connection name with PostgreSQL. Type org.postgresql.Driver in the driver class text box. The URL should be jdbc:postgresql:foo in our case. Then fill in the appropriate information for your current Mac OS user.
|
|
Click the test button to verify that your connection is working, dismiss the connection test window, then click on the OK button in the new connection window and the ViennaSQL options window. There is a combo box in the top right-hand corner of ViennaSQL's main window -- select PostgreSQL from that box. This will connect you to the database. Paste the following SQL into the lower pane of ViennaSQL's window:
CREATE TABLE books (
code CHARACTER(13) CONSTRAINT firstkey PRIMARY KEY,
title CHARACTER VARYING(40) NOT NULL,
author CHARACTER VARYING(40) NOT NULL,
price DECIMAL(4,2),
kind CHARACTER VARYING(10)
);
Select Query and Execute from the menu bar. Then select Query and Commit from the menu bar. You've now created your first table, congratulations again! Now, let's populate it with some sample data. Paste the following six lines of code in the lower pane, replacing what is already there:
INSERT INTO books (code, title, author, price, kind)
VALUES ('1-56592-846-6', 'Practical PostgreSQL', 'John C. Worsley, Joshua D. Drake', 44.95, 'database');
INSERT INTO books (code, title, author, price, kind)
VALUES ('1-56592-616-1', 'Database Programming with JDBC and Java', 'George Reese', 34.95, 'java');
INSERT INTO books (code, title, author, price, kind)
VALUES ('0-596-00160-6', 'Learning Cocoa', 'Apple Computer', 34.95, 'macintosh');
You'll need to execute and commit these statements just like you did the last one.
|
|
Now, in order to view the data we just inserted, let's check out some Java code! I've written some Java that will connect to our database and return some values from each row of the books table. Download connectPostgreSQL.java to your desktop. Now, in Terminal switch to your desktop folder and compile the source. Then run the resulting Java class. Here's what I had to enter into Terminal to accomplish this:
cd ~/Desktop
javac connectPostgreSQL.java
java connectPostgreSQL
After hitting enter on the third command line, you should see output similar to what you see below:
Practical PostgreSQL costs 44.95
Database Programming with JDBC and Java costs 34.95
Learning Cocoa costs 34.95
And that's all I wrote. Well, almost. I've included some links below for more information on PostgreSQL and some of the technologies that were touched on in this article.
More Information
PostgreSQL SQL Commands at PostgreSQL's Interactive Documentation site has more information on what SQL is supported by PostgreSQL.
The sample chapter for Learning Unix for Mac OS X by Dave Taylor and Jerry Peek has information on creating a .tcshrc file to make environment variable changes permanent. You may also want to look into Sun's JDBC pages.
Michael Brewer is a developer based near Charlotte, North Carolina. His interests include web development of various flavors, databases, and Java. One of the off-shoots of these activities is his website Brewed Thoughts.
Return to the MacDevCenter.com.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 15 of 15.
-
jdbc
2007-04-16 00:33:42 haaaaaaa [Reply | View]
how to solve this problem in linux
java.lang.ClassNotFoundException: org.postgresql.Driver not found in gnu.gcj.runtime.SystemClassLoader{urls=[file:./], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}
-
may seem obvious
2003-09-15 18:44:39 anonymous2 [Reply | View]
but everwhere in code samples where it says 'logfile' they really mean that you should specify your preferred logfile location. How does one choose a good logfile location? Beats me.
eg
/Users/Shared/PostgreSQL/bin/pg_ctl -D /Users/Shared/PostgreSQL/data -l [specify preferred logfile] start
-
can't create a new DB
2003-07-30 02:43:11 anonymous2 [Reply | View]
Thank a lot for your article, its very usefull for me, i do every step right since the command "createdb foo", terminal give me this message:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
createdb: database creation failed
do you know why?
Thanks a lot -
can't create a new DB
2004-01-03 13:59:08 anonymous2 [Reply | View]
The Postmaster has to be running before you can issue commands. The easiest way to start the postmaster is to install the Postgres Startup item created by Marc Liyanage which you can find at this page:
http://www.entropy.ch/software/macosx/postgresql/
It may work for you out of the box; but if it doesn't, check his User Forums for information on how to fix the problem.
-
problem during ./configure...make
RE: problem during ./configure...make
2002-09-22 11:13:30 anonymous2 [Reply | View]
Did you install the developer tools? It is the 3rd CD for Jaguar...
-
Postgres & Perl DBI: DBD::Pg
2002-09-12 13:51:10 anonymous2 [Reply | View]
Any clues why make test of the DBD::Pg driver fails?
I want to use perl with my postgresql!!
-
problem during ./configure...make
2002-09-01 20:40:43 ctucker10 [Reply | View]
Trying to install on iBook with Mac OS 10.2. Here's what happened:
[Curtis-Tuckers-Computer:postgre_sql_info/download/postgresql-7.2.2] ctucker10% ./configure --prefix=/users/shared/PostgreSQL --enable-locale
loading cache ./config.cache
checking host system type... powerpc-apple-darwin6.0
checking which template to use... darwin
checking whether to build with locale support... yes
checking whether to build with recode support... no
checking whether to build with multibyte character support... no
checking whether NLS is wanted... no
checking for default port number... 5432
checking for default soft limit on number of connections... 32
checking for gcc... no
checking for cc... no
configure: error: no acceptable cc found in $PATH
[Curtis-Tuckers-Computer:postgre_sql_info/download/postgresql-7.2.2] ctucker10% make
make: Command not found.
[Curtis-Tuckers-Computer:postgre_sql_info/download/postgresql-7.2.2] ctucker10%
I'm a long time mac user but, new to unix. Any help will be greatly appreciated. Thanks.
Curtis Tucker -
problem during ./configure...make
2004-01-03 13:54:21 anonymous2 [Reply | View]
Well, this is a really old message; but for anyone reading with the same problem: you didn't load the Apple Developer's Tools. You don't have gcc or cc, which is the Gnu C Compiler needed compile the source code.
The Developer Tools are available on a CD which comes with Jaguar or Panther.
-
CLASSPATH: Undefined variable.
2002-06-26 19:44:24 Michael Brewer | [Reply | View]
I got an e-mail from one of the readers of this article that described difficulty they had setting their classpath -- they got an error stating that classpath was an undefined variable. In the article, I recommended setting the classpath with the following command:
setenv CLASSPATH ${CLASSPATH}:/Library/Java/Extensions/postgresql.jar:.
I do not remember having trouble with this command under Mac OS X 10.1.4. However, I received the reader's e-mail after upgrading to 10.1.5 and am able to reproduce it in that version. What you need to do since classpath might not already be defined is type the following instead of what appears in the article.
setenv CLASSPATH /Library/Java/Extensions/postgresql.jar:.
-
Easier way to install
2002-06-10 12:18:33 gaspode [Reply | View]
Look at http://fink.sf.net and search for postgresql package.
-
Importing from SQL Server
2002-06-10 10:37:36 vineetb [Reply | View]
Excellent article!!
Now only if there was a way to import my existing SQL 2K database into PostgresSQL....
Any ideas? -
Importing from SQL Server
2002-06-12 04:15:29 Michael Brewer | [Reply | View]
I haven't looked into any bulk copy procedures for this type of thing. But, off the top of my head there is a JDBC driver available from Microsoft for SQL Server 2000, so it shouldn't be much trouble to connect to your SQL Server with Java or one of the tools I used in my article to transfer your data.
For instance, Vienna SQL will let you export the results of a query as a comma separated values file. You should then be able to use PostgreSQL's copy command to get the data in.
A quick search on Google also returned this handy utility: http://www.daveorr.fsnet.co.uk/csvbridge/
-
prepackaged version available
2002-06-09 09:26:48 gcole [Reply | View]
The nitty-gritty installation info is helpful, but the easy way is to use the Mac OS X package created by Marc Liyanage; it's available from his site at http://www.entropy.ch/software/macosx/postgresql/.









93-80-29-243:PostgreSQL8 olgaryzikova$ su - postgres
Password:
93-80-29-243:PostgreSQL8 olgaryzikova$
If i understand correctly i can only start PosgreSQL server under the postgres user - but i can't switch to this user and also do not see it in my System preferences --> Accounts...
May be somebody had similar problems? How do you think it is possible to solve it?
Thanks!