Thread: IF EXISTS
Hi, I would like to know if "IF EXISTS" exists under postgresql ? because i did not find it. before to create users or database, i would like to be sure that they do not exist already. so how can i test it and do something like : IF EXISTS database "test" DROP database "test"; thanks a lot, Maileen __________________________________ Yahoo! FareChase: Search multiple travel sites in one click. http://farechase.yahoo.com
On Mon, Nov 14, 2005 at 13:20:59 -0800, "P.M" <pmdanger@yahoo.com> wrote: > Hi, > > I would like to know if "IF EXISTS" exists under > postgresql ? > because i did not find it. > > before to create users or database, i would like to be > sure that they do not exist already. > > so how can i test it and do something like : > > IF EXISTS database "test" DROP database "test"; I don't believe there is a feature like that for users (roles) or databases. The normal thing to do would be to have the application try to delete the object and ignore the problem if things fail. If this is happening in a larger transaction that you don't want rolled back, then you can use savepoints.
On 11/14/05, P.M <pmdanger@yahoo.com> wrote: > Hi, > > I would like to know if "IF EXISTS" exists under > postgresql ? > because i did not find it. > > before to create users or database, i would like to be > sure that they do not exist already. > > so how can i test it and do something like : > > IF EXISTS database "test" DROP database "test"; > > thanks a lot, > Maileen > > something in shell is fine? ---------SCRIPT BEGIN HERE ------------ #!/bin/sh psql template1 -A -t -c "SELECT datname FROM pg_database WHERE datname = 'test'" | while read D ; do dropdb test # or you can do # psql template1 -c "DROP DATABASE test" # whatever you feel comfortable with done createdb test ---------SCRIPT END HERE ------------ -- regards, Jaime Casanova (DBA: DataBase Aniquilator ;)
How about if exists (select .. from ..) On 11/14/05, Jaime Casanova <systemguards@gmail.com> wrote: > On 11/14/05, P.M <pmdanger@yahoo.com> wrote: > > Hi, > > > > I would like to know if "IF EXISTS" exists under > > postgresql ? > > because i did not find it. > > > > before to create users or database, i would like to be > > sure that they do not exist already. > > > > so how can i test it and do something like : > > > > IF EXISTS database "test" DROP database "test"; > > > > thanks a lot, > > Maileen > > > > > > something in shell is fine? > > ---------SCRIPT BEGIN HERE ------------ > #!/bin/sh > > psql template1 -A -t -c "SELECT datname FROM pg_database WHERE > datname = 'test'" | while read D ; do > dropdb test > # or you can do > # psql template1 -c "DROP DATABASE test" > # whatever you feel comfortable with > done > > createdb test > ---------SCRIPT END HERE ------------ > > > -- > regards, > Jaime Casanova > (DBA: DataBase Aniquilator ;) > > ---------------------------(end of broadcast)--------------------------- > TIP 9: In versions below 8.0, the planner will ignore your desire to > choose an index scan if your joining column's datatypes do not > match >