Page 1 of 1

Creating a new PostgreSQL database

Posted: Thu Mar 20, 2014 10:55 am
by Simon Knight
I am working on a database driven application that will act as a client to a postgresql database running on a windows server. At present I have postgresql running on my "localhost" on my Mac OS X development machine.

I am looking for a simple, elegant and robust method of dealing with the situation where my code attempts to connect to a database that does not exist. In these circumstances the command revOpenDatabase will return with an error message along the lines of "FATAL: database "myDatabase" does not exist" which is simply trapped. My initial thoughts were to attempt to write code that creates the database but I don't know how to. I have tried the shell command and createdb but am unable to get it to work even with localhost and suspect that issuing the command from a client to a server will be difficult.

My solution at present is to issue a warning that tells the user to run pgAdmin on the server and create the database. However I am curious if there is a better way.

My attempts at using the shell command

Code: Select all

on mouseUp
   
   --shell ("createdb smkTestDb")  -- fails
   --shell ("ls -l *.txt")  -- dictionary example also fails
   --put the shell of "ls -l *.txt" into field "debug"  --fails
      put the shell of "ls" into field "debug"  --lists my applications folder
   answer "Answer: " & it
   answer the result
end mouseUp

Re: Creating a new PostgreSQL database

Posted: Wed Mar 26, 2014 2:21 pm
by MaxV
You must supply a valid database name, the default Postrgre configuration contains a pg (?) database. So you can use it to connect then create your database using a SQL query.