Received: from localhost (maia-4.hub.org [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id A852C9FA103 for ; Tue, 5 Dec 2006 08:26:29 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-new, port 10024) with ESMTP id 31255-04 for ; Tue, 5 Dec 2006 08:26:24 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from nf-out-0910.google.com (nf-out-0910.google.com [64.233.182.184]) by postgresql.org (Postfix) with ESMTP id 8AC359F9EED for ; Tue, 5 Dec 2006 08:26:24 -0400 (AST) Received: by nf-out-0910.google.com with SMTP id x4so204718nfb for ; Tue, 05 Dec 2006 04:26:22 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:user-agent:mime-version:to:subject:content-type:content-transfer-encoding; b=F3uKNoXmcBRWNy4FDT2simmkhrXjM+We5S/Jon2RvLn9/VulxXWF7l9jkrif2bJWzTnvanqSyUgxo/jnsjgIfCwSk6RecbV6uniBzyqZjIVj1XKcHjR2CU2HweMvX3Q32rI5bdARRZ3AGoscYACe8WGjv46QSUxBvRlihAPIIBw= Received: by 10.48.210.20 with SMTP id i20mr742754nfg.1165321579550; Tue, 05 Dec 2006 04:26:19 -0800 (PST) Received: from ?172.25.18.10? ( [41.243.49.18]) by mx.google.com with ESMTP id l21sm2401509nfc.2006.12.05.04.26.17; Tue, 05 Dec 2006 04:26:18 -0800 (PST) Message-ID: <457565B4.80301@gmail.com> Date: Tue, 05 Dec 2006 14:27:32 +0200 From: Brendon Bentley User-Agent: Thunderbird 1.5.0.5 (Windows/20060719) MIME-Version: 1.0 To: "PostgreSQL JDBC" Subject: Re: Use Driver to Create database? Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200612/37 X-Sequence-Number: 16269 How to create database with JDBC on PostgreSQL RDMS: For a complete example that works: //-- Note that this string ends on "postgres". That is // the name of the default database that can always // be connected to. String dbUrl = "jdbc:postgresql://localhost:8988/postgres"; String dbDriver = "org.postgresql.Driver"; String dbUsername = "Db_test_client"; String dbPassword = "Db_test_pwd"; try { //-- Checks that database driver is loaded Class.forName(dbDriver); } catch (ClassNotFoundException ex) { throw new DbWriteException( ex ); } try { //-- Connect to database Connection dbConnection = DriverManager.getConnection(dbUrl,dbUsername,dbPassword); //--Make SURE auto commit is on, because you cannot // add a database to a transaction block. A trans- // action block is a collection of SQL statements // that are only executed when dbConnection.commit() // is called. dbConnection.setAutoCommit(true); String sql = "CREATE DATABASE testDatabase"; Statement cs = dbConnection.createStatement(); cs.execute(sql); cs.close(); dbConnection.close(); //-- If this code still fails your login account probably // does not have privileges to create databases On 2006-11-19 21:42, Roland Walter wrote: > Charlie Kelly wrote: > > Is it possible to use the driver to create a new database (inside a java > > program), > > or is it necessary to first create the database using the createdb utility? > > > > If it is possible to use the driver, what is the correct syntax? > > > > There is a SQL-command for that, look here: > > > http://www.postgresql.org/docs/8.1/interactive/sql-createdatabase.html > > First connect to the postgres or template1 schema of the cluster, that > you created with initdb. Execute the "CREATE DATABASE". Reconnect to the > database you created. > > Regards, > Roland. > - -- > Dipl.-Phys. Roland Walter > mailto: roland (dot)walter (dot) rwa (at) gmx (dot) net > > ---------------------------(end of broadcast)--------------------------- > TIP 7: You can help support the PostgreSQL project by donating at > > http://www.postgresql.org/about/donate >