create database if not exists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shweta gandhi
    New Member
    • Apr 2007
    • 7

    create database if not exists

    Someone know how to create database in postgreSQL through Java code?
    How to check particular database exist or not in postgreSQL through java code?
  • michaelb
    Recognized Expert Contributor
    • Nov 2006
    • 534

    #2
    The query you need to execute is not depending on the client API, whether it is Java, C, or anything else.
    Look at this thread for details.

    It may be also helpful to review documentation on Create database and Information Schema

    Comment

    • shweta gandhi
      New Member
      • Apr 2007
      • 7

      #3
      thanks for reply.
      but i want to create database dynamically through jdbc .
      in java to get connected we have to enter database name in getconnection() method.

      [ Connection con = DriverManager.g etConnection("j dbc:postgresql://IpAddress:5432/" + databasename, dbusername, dbpassword); ]

      but sice we don't have any database how to? & where to get connected?
      how to create database at run time??

      Comment

      • michaelb
        Recognized Expert Contributor
        • Nov 2006
        • 534

        #4
        Typically you would use external utility createdb, but if you want to do it in Java, first time you can connect to template1, but once you have created some database you can connect to it and create new ones.

        See more details here

        Comment

        • shweta gandhi
          New Member
          • Apr 2007
          • 7

          #5
          hi
          thanks ur soln worked.
          but How to check particular database exist or not in postgreSQL through java code?

          Comment

          • shweta gandhi
            New Member
            • Apr 2007
            • 7

            #6
            hi
            thanks ur soln worked.
            but How to check particular database exist or not in postgreSQL through java code? i.e. how to compare two databases names and structure?

            Comment

            • michaelb
              Recognized Expert Contributor
              • Nov 2006
              • 534

              #7
              Originally posted by shweta gandhi
              hi
              thanks ur soln worked.
              but How to check particular database exist or not in postgreSQL through java code? i.e. how to compare two databases names and structure?
              I'm not sure what "ur soln" means...
              To get the list of the databases you can run this query:

              Code:
              select datname from pg_catalog.pg_database;
              if all you need is to find out whether a particular database exists you can modify this query a bit, something like this will work:

              Code:
              select count(*) from pg_catalog.pg_database where datname = 'some_name';
              I think there are some third-party tools that may help you with comparing databases, I don't have any specific pointers, but if you spend some time with Google you will find few.

              If you don't want to employ any third-party tools you need to define exactly what you mean by "comparing the database structures"
              Does this include just a list of tables, or also other objects, such as views, functions, triggers, rules, etc.

              As I mentioned earlier studying the manual on Information schema may be very helpful to you.

              Comment

              • nlshriraam
                New Member
                • May 2007
                • 1

                #8
                Originally posted by shweta gandhi
                thanks for reply.
                but i want to create database dynamically through jdbc .
                in java to get connected we have to enter database name in getconnection() method.

                [ Connection con = DriverManager.g etConnection("j dbc:postgresql://IpAddress:5432/" + databasename, dbusername, dbpassword); ]

                but sice we don't have any database how to? & where to get connected?
                how to create database at run time??
                prepare a class name and do the follow code

                //public final static String DRIVER = "com.mysql.jdbc .Driver";
                //public final static String URL = "jdbc:mysql ://localhost/IA";
                //public final static String USER = "";
                //public final static String PASSWORD = "";

                private static Properties dbprops = new Properties();
                private static Driver myDriver = null;
                private static Connection connection = null;
                private static Statement statement1 = null;
                public static void init() {

                dbprops.put("us er", USER);
                dbprops.put("pa ssword", PASSWORD);
                try
                {
                myDriver = (Driver)Class.f orName(DRIVER). newInstance();
                connection = DriverManager.g etConnection(UR L, dbprops);
                try
                {
                Statement st = connection.crea teStatement();
                BufferedReader bf = new BufferedReader( new InputStreamRead er(System.in));
                String database = DATABASENAME; // "nlshriraam ";//bf.readLine();
                st.executeUpdat e("CREATE DATABASE "+database) ;

                }
                catch (SQLException s)
                {

                }
                }
                catch (Exception e)
                {


                }
                return;
                }


                hope this work
                regards
                NLShriraam

                Comment

                Working...