Mitch Vincent wrote:
> I'm fairly new to PostreSQL, coming from MySQL. My Python application
> generates these MySQL commands:
>
> drop database if exists Foo;
> create database Foo;
> use Foo;
> Using the PostgreSQL online docs, my closest translation is:
>
> drop database Foo;
> create database Foo; You supply the database name on connect.. I don't *think* you can change
databases after you've connected but I could be very wrong (if you can, I'd
sure like to know!)
If you're running PostgreSQL interactively (via the "psql" command) you can connect to any database in the installation, provided the user in question has been granted rights to access the database, by using the "\connect" command. The format is "\connect dbname".
> What I'm missing is:
> * "if exists" for the drop, in order to avoid an error the very first time
> this program runs
> * the "use Foo;"
>
> Can I do these things in PostreSQL from SQL (e.g., not from the command
line)?
Not sure on these...
There is no "if exists" functionality in PostgreSQL. However, you could probably create your own "if exists" type of functionality. I've found that it's not really necessary. I access my databases either interactively, via psql, via the Perl Pg module, or via PHP. If you attempt to drop a database that doesn't exist, PostgreSQL will issue an error message. If you're running interactively, you'll see the message; if you're accessing via a Perl module or PHP, you can check the query results to see if an error occurred. I'm fairly certain that this same mechanism exists if you're using C or C++ to access your databases.
I'm not familiar with the "use Foo" functionality of MySQL, so I can't discuss it.
-Mitch
--
John Burski
Chief IT Cook and Bottlewasher
911 Emergency Products, St. Cloud, MN
(320) 656 0076 www.911ep.com
++++++++++++++++++++++++++++++++++
+ How's your cheese holding out? +
++++++++++++++++++++++++++++++++++