pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Use Driver to Create database?
6+ messages / 5 participants
[nested] [flat]

* Use Driver to Create database?
@ 2006-11-19 19:45  Charlie Kelly <Charlie@CharlieKelly.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Charlie Kelly @ 2006-11-19 19:45 UTC (permalink / raw)
  To: pgsql-jdbc

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?

Thanks

Charlie Kelly





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Use Driver to Create database?
@ 2006-11-19 21:42  Roland Walter <roland.walter.rwa@gmx.net>
  parent: Charlie Kelly <Charlie@CharlieKelly.com>
  0 siblings, 2 replies; 6+ messages in thread

From: Roland Walter @ 2006-11-19 21:42 UTC (permalink / raw)
  To: Charlie Kelly <Charlie@CharlieKelly.com>; +Cc: pgsql-jdbc

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFFYM+vxoOEAkary1ERAmASAJ0YfU9L9hepz68I0A2/yzV3ZjSAjwCeOnqU
LqefeHLpkkc2Np7cRYVmawY=
=4SOY
-----END PGP SIGNATURE-----



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Use Driver to Create database?
@ 2006-11-19 21:46  Roland Walter <roland.walter.rwa@gmx.net>
  parent: Roland Walter <roland.walter.rwa@gmx.net>
  1 sibling, 0 replies; 6+ messages in thread

From: Roland Walter @ 2006-11-19 21:46 UTC (permalink / raw)
  To: Charlie Kelly <Charlie@CharlieKelly.com>; +Cc: pgsql-jdbc

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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

Oops, the word schema is wrong, postgres and template1 are default
databases.

- --
Dipl.-Phys. Roland Walter
mailto: roland (dot) walter (dot) rwa (at) gmx (dot) net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFFYNC9xoOEAkary1ERApjXAJ4tGByZErjSwabxWlH5P+dq95cpNQCdFhP4
S/b2XDCr+I2MWKH5kxEcYGw=
=Ddbc
-----END PGP SIGNATURE-----



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Use Driver to Create database?
@ 2006-11-19 22:04  Heikki Linnakangas <heikki@enterprisedb.com>
  parent: Roland Walter <roland.walter.rwa@gmx.net>
  1 sibling, 1 reply; 6+ messages in thread

From: Heikki Linnakangas @ 2006-11-19 22:04 UTC (permalink / raw)
  To: Roland Walter <roland.walter.rwa@gmx.net>; +Cc: Charlie Kelly <Charlie@CharlieKelly.com>; pgsql-jdbc

Roland Walter wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> 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.

You don't want to connect to template1, though, if the program is going 
to be used concurrently by more than one use. Template1 is used as 
template (hence the name) to make the new database, and it will throw an 
error if there's any other users connected to it beside the one issuing 
the CREATE DATABASE command.

-- 
   Heikki Linnakangas
   EnterpriseDB   http://www.enterprisedb.com



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Use Driver to Create database?
@ 2006-11-20 05:07  John R Pierce <pierce@hogranch.com>
  parent: Heikki Linnakangas <heikki@enterprisedb.com>
  0 siblings, 0 replies; 6+ messages in thread

From: John R Pierce @ 2006-11-20 05:07 UTC (permalink / raw)
  To: Charlie Kelly <Charlie@CharlieKelly.com>; +Cc: pgsql-jdbc


>> 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.
>
> You don't want to connect to template1, though, if the program is 
> going to be used concurrently by more than one use. Template1 is used 
> as template (hence the name) to make the new database, and it will 
> throw an error if there's any other users connected to it beside the 
> one issuing the CREATE DATABASE command.
>

indeed.  In fact you can connect to ANY existing database prior to 
issusing the CREATE DATABASE command.  It is important, however, that 
you be logged on with credentials that have the CREATEDB privilege





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Use Driver to Create database?
@ 2006-12-05 12:27  Brendon Bentley <bfbentley@gmail.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Brendon Bentley @ 2006-12-05 12:27 UTC (permalink / raw)
  To: pgsql-jdbc

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
 >



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2006-12-05 12:27 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-11-19 19:45 Use Driver to Create database? Charlie Kelly <Charlie@CharlieKelly.com>
2006-11-19 21:42 ` Roland Walter <roland.walter.rwa@gmx.net>
2006-11-19 21:46   ` Roland Walter <roland.walter.rwa@gmx.net>
2006-11-19 22:04   ` Heikki Linnakangas <heikki@enterprisedb.com>
2006-11-20 05:07     ` John R Pierce <pierce@hogranch.com>
2006-12-05 12:27 Re: Use Driver to Create database? Brendon Bentley <bfbentley@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox