Обсуждение: Creating DB programmatically?

Поиск
Список
Период
Сортировка

Creating DB programmatically?

От
Roy's Email
Дата:
I would like to create my DB programmatically.  I.e. If the DB does not already exist, I would like to be able to create (and populate) it from within my application.  Is there some way to do this?  Everything I see seems to require connecting to an existing DB before you can do anything.

Regards,

- Roy

=*=*=
Always do right.
This will gratify some people and astonish the rest. - Mark Twain

The truth is rarely pure, and never simple. - Oscar Wilde
 

Re: Creating DB programmatically?

От
Jayadevan M
Дата:
Hello,
>
> I would like to create my DB programmatically.  I.e. If the DB does
> not already exist, I would like to be able to create (and populate)
> it from within my application.  Is there some way to do this?  
> Everything I see seems to require connecting to an existing DB
> before you can do anything.


If it is unix/linux, couldn't you do

[postgres@tcctest ~]$ psql -l | grep mydatabase
and then create one if it did not fetch any data?
If it exists, you will get something like
 mydatabase | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |

Regards,
Jayadevan





DISCLAIMER:


"The information in this e-mail and any attachment is intended only for the person to whom it is addressed and may contain confidential and/or privileged material. If you have received this e-mail in error, kindly contact the sender and destroy all copies of the original communication. IBS makes no warranty, express or implied, nor guarantees the accuracy, adequacy or completeness of the information contained in this email or any attachment and is not liable for any errors, defects, omissions, viruses or for resultant loss or damage, if any, direct or indirect."





Re: Creating DB programmatically?

От
Roy's Email
Дата:
This is on Windows, I'm afraid.

Regards,

- Roy

=*=*=
Always do right.
This will gratify some people and astonish the rest. - Mark Twain

The truth is rarely pure, and never simple. - Oscar Wilde
 

 

To: rmw256@hotmail.com
CC: pgsql-novice@postgresql.org; pgsql-novice-owner@postgresql.org
Subject: Re: [NOVICE] Creating DB programmatically?
From: Jayadevan.Maymala@ibsplc.com
Date: Tue, 12 Jul 2011 08:46:01 +0530

Hello,
>
> I would like to create my DB programmatically.  I.e. If the DB does
> not already exist, I would like to be able to create (and populate)
> it from within my application.  Is there some way to do this?  
> Everything I see seems to require connecting to an existing DB
> before you can do anything.


If it is unix/linux, couldn't you do

[postgres@tcctest ~]$ psql -l | grep mydatabase
and then create one if it did not fetch any data?
If it exists, you will get something like
 mydatabase | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |

Regards,
Jayadevan





DISCLAIMER:


"The information in this e-mail and any attachment is intended only for the person to whom it is addressed and may contain confidential and/or privileged material. If you have received this e-mail in error, kindly contact the sender and destroy all copies of the original communication. IBS makes no warranty, express or implied, nor guarantees the accuracy, adequacy or completeness of the information contained in this email or any attachment and is not liable for any errors, defects, omissions, viruses or for resultant loss or damage, if any, direct or indirect."





Re: Creating DB programmatically?

От
Merlin Moncure
Дата:
On Mon, Jul 11, 2011 at 11:00 PM, Roy's Email <rmw256@hotmail.com> wrote:
> This is on Windows, I'm afraid.

well, I have good news --  both psql and grep work just fine on
windows :-).  unfortunately batch is a pretty horrible scriptring
language, but if you need to get real work done you can find a
standalone .exe (like bash.exe, grep.exe, find.exe etc).

merlin

Re: Creating DB programmatically?

От
Jayadevan M
Дата:
Hello,

>
> On Mon, Jul 11, 2011 at 11:00 PM, Roy's Email <rmw256@hotmail.com> wrote:
> > This is on Windows, I'm afraid.
>
> well, I have good news --  both psql and grep work just fine on
> windows :-).  unfortunately batch is a pretty horrible scriptring
> language, but if you need to get real work done you can find a
> standalone .exe (like bash.exe, grep.exe, find.exe etc).
>
May be

http://gnuwin32.sourceforge.net/packages/coreutils.htm
But anyway, when we use psql, we are connecting to postgresql.
Regards,
Jayadevan





DISCLAIMER:


"The information in this e-mail and any attachment is intended only for the person to whom it is addressed and may contain confidential and/or privileged material. If you have received this e-mail in error, kindly contact the sender and destroy all copies of the original communication. IBS makes no warranty, express or implied, nor guarantees the accuracy, adequacy or completeness of the information contained in this email or any attachment and is not liable for any errors, defects, omissions, viruses or for resultant loss or damage, if any, direct or indirect."





Re: Creating DB programmatically?

От
Roy's Email
Дата:
OK, based on what I'm hearing, I'm clearly missing something.  I'm looking to create the DB from within my application; not from some shell/command line window.  Also, I want my application to run on "any" Windows machine, so I can't dictate ancillary programs other than requiring that postgreSQL be present.

Regards,

- Roy

=*=*=
Always do right.
This will gratify some people and astonish the rest. - Mark Twain

The truth is rarely pure, and never simple. - Oscar Wilde
 

 

To: rmw256@hotmail.com
CC: pgsql-novice@postgresql.org; pgsql-novice-owner@postgresql.org
Subject: Re: [NOVICE] Creating DB programmatically?
From: Jayadevan.Maymala@ibsplc.com
Date: Tue, 12 Jul 2011 10:49:17 +0530

Hello,

>
> On Mon, Jul 11, 2011 at 11:00 PM, Roy's Email <rmw256@hotmail.com> wrote:
> > This is on Windows, I'm afraid.
>
> well, I have good news --  both psql and grep work just fine on
> windows :-).  unfortunately batch is a pretty horrible scriptring
> language, but if you need to get real work done you can find a
> standalone .exe (like bash.exe, grep.exe, find.exe etc).
>
May be

http://gnuwin32.sourceforge.net/packages/coreutils.htm
But anyway, when we use psql, we are connecting to postgresql.
Regards,
Jayadevan





DISCLAIMER:


"The information in this e-mail and any attachment is intended only for the person to whom it is addressed and may contain confidential and/or privileged material. If you have received this e-mail in error, kindly contact the sender and destroy all copies of the original communication. IBS makes no warranty, express or implied, nor guarantees the accuracy, adequacy or completeness of the information contained in this email or any attachment and is not liable for any errors, defects, omissions, viruses or for resultant loss or damage, if any, direct or indirect."





Re: Creating DB programmatically?

От
Michael Wood
Дата:
On 12 July 2011 08:48, Roy's Email <rmw256@hotmail.com> wrote:
> OK, based on what I'm hearing, I'm clearly missing something.  I'm looking
> to create the DB from within my application; not from some shell/command
> line window.  Also, I want my application to run on "any" Windows machine,
> so I can't dictate ancillary programs other than requiring that postgreSQL
> be present.
Try connecting to e.g. the "template1" database in order to check for
existence of your database and issue "CREATE DATABASE ..." from there.

> ________________________________
> To: rmw256@hotmail.com
> CC: pgsql-novice@postgresql.org; pgsql-novice-owner@postgresql.org
> Subject: Re: [NOVICE] Creating DB programmatically?
> From: Jayadevan.Maymala@ibsplc.com
> Date: Tue, 12 Jul 2011 10:49:17 +0530
>
> Hello,
>
>>
>> On Mon, Jul 11, 2011 at 11:00 PM, Roy's Email <rmw256@hotmail.com> wrote:
>> > This is on Windows, I'm afraid.
>>
>> well, I have good news --  both psql and grep work just fine on
>> windows :-).  unfortunately batch is a pretty horrible scriptring
>> language, but if you need to get real work done you can find a
>> standalone .exe (like bash.exe, grep.exe, find.exe etc).
>>
> May be
> http://gnuwin32.sourceforge.net/packages/coreutils.htm
> But anyway, when we use psql, we are connecting to postgresql.
> Regards,
> Jayadevan

--
Michael Wood <esiotrot@gmail.com>

Re: Creating DB programmatically?

От
Roy's Email
Дата:
Now I am particularly confused.  How do I issue "CREATE DATABASE ..." from my program?  I thought that I needed a connection (PGconn) before I could execute a command and I don't believe I have a valid connection if my attempt to connect to a (non-existent) DB fails.  [Does it change anything that I'm writing in C++ and using the 'libpq' interface?]

Regards,

- Roy

=*=*=
Always do right.
This will gratify some people and astonish the rest. - Mark Twain

The truth is rarely pure, and never simple. - Oscar Wilde
 

 
> From: esiotrot@gmail.com
> Date: Tue, 12 Jul 2011 09:12:04 +0200
> Subject: Re: [NOVICE] Creating DB programmatically?
> To: rmw256@hotmail.com
> CC: pgsql-novice@postgresql.org
>
> On 12 July 2011 08:48, Roy's Email <rmw256@hotmail.com> wrote:
> > OK, based on what I'm hearing, I'm clearly missing something.  I'm looking
> > to create the DB from within my application; not from some shell/command
> > line window.  Also, I want my application to run on "any" Windows machine,
> > so I can't dictate ancillary programs other than requiring that postgreSQL
> > be present.
> Try connecting to e.g. the "template1" database in order to check for
> existence of your database and issue "CREATE DATABASE ..." from there.
>
> > ________________________________
> > To: rmw256@hotmail.com
> > CC: pgsql-novice@postgresql.org; pgsql-novice-owner@postgresql.org
> > Subject: Re: [NOVICE] Creating DB programmatically?
> > From: Jayadevan.Maymala@ibsplc.com
> > Date: Tue, 12 Jul 2011 10:49:17 +0530
> >
> > Hello,
> >
> >>
> >> On Mon, Jul 11, 2011 at 11:00 PM, Roy's Email <rmw256@hotmail.com> wrote:
> >> > This is on Windows, I'm afraid.
> >>
> >> well, I have good news --  both psql and grep work just fine on
> >> windows :-).  unfortunately batch is a pretty horrible scriptring
> >> language, but if you need to get real work done you can find a
> >> standalone .exe (like bash.exe, grep.exe, find.exe etc).
> >>
> > May be
> > http://gnuwin32.sourceforge.net/packages/coreutils.htm
> > But anyway, when we use psql, we are connecting to postgresql.
> > Regards,
> > Jayadevan
>
> --
> Michael Wood <esiotrot@gmail.com>
>
> --
> Sent via pgsql-novice mailing list (pgsql-novice@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-novice

Re: Creating DB programmatically?

От
Thomas Kellerer
Дата:
Roy's Email, 12.07.2011 01:40:
> I would like to create my DB programmatically. I.e. If the DB does
> not already exist, I would like to be able to create (and populate)
> it from within my application. Is there some way to do this?
> Everything I see seems to require connecting to an existing DB before
> you can do anything.
>

Connect to one of the template databases (template0, template1) from within your program (those databases are always
there).
Then you have a valid connection through which you can execute the CREATE DATABASE statement.

You can also run a "SELECT datname FROM pg_database" using that connection to test if your database is already there.

Thomas



Re: Creating DB programmatically?

От
Michael Wood
Дата:
On 12 July 2011 09:31, Roy's Email <rmw256@hotmail.com> wrote:
> Now I am particularly confused.  How do I issue "CREATE DATABASE ..." from

Well, I've never tried it myself, but you can do it from "psql" and
that connects to Postgres in order to do it.

> my program?  I thought that I needed a connection (PGconn) before I could
> execute a command and I don't believe I have a valid connection if my
> attempt to connect to a (non-existent) DB fails.  [Does it change anything

That's why I said you should first connect to the "template1"
database, which will exist.

> that I'm writing in C++ and using the 'libpq' interface?]
>
>> From: esiotrot@gmail.com
>> Date: Tue, 12 Jul 2011 09:12:04 +0200
>> Subject: Re: [NOVICE] Creating DB programmatically?
>> To: rmw256@hotmail.com
>> CC: pgsql-novice@postgresql.org
>>
>> On 12 July 2011 08:48, Roy's Email <rmw256@hotmail.com> wrote:
>> > OK, based on what I'm hearing, I'm clearly missing something.  I'm
>> > looking
>> > to create the DB from within my application; not from some shell/command
>> > line window.  Also, I want my application to run on "any" Windows
>> > machine,
>> > so I can't dictate ancillary programs other than requiring that
>> > postgreSQL
>> > be present.
>> Try connecting to e.g. the "template1" database in order to check for
>> existence of your database and issue "CREATE DATABASE ..." from there.
>>
>> > ________________________________
>> > To: rmw256@hotmail.com
>> > CC: pgsql-novice@postgresql.org; pgsql-novice-owner@postgresql.org
>> > Subject: Re: [NOVICE] Creating DB programmatically?
>> > From: Jayadevan.Maymala@ibsplc.com
>> > Date: Tue, 12 Jul 2011 10:49:17 +0530
>> >
>> > Hello,
>> >
>> >>
>> >> On Mon, Jul 11, 2011 at 11:00 PM, Roy's Email <rmw256@hotmail.com>
>> >> wrote:
>> >> > This is on Windows, I'm afraid.
>> >>
>> >> well, I have good news --  both psql and grep work just fine on
>> >> windows :-).  unfortunately batch is a pretty horrible scriptring
>> >> language, but if you need to get real work done you can find a
>> >> standalone .exe (like bash.exe, grep.exe, find.exe etc).
>> >>
>> > May be
>> > http://gnuwin32.sourceforge.net/packages/coreutils.htm
>> > But anyway, when we use psql, we are connecting to postgresql.
>> > Regards,
>> > Jayadevan

--
Michael Wood <esiotrot@gmail.com>

Re: Creating DB programmatically?

От
Michael Wood
Дата:
On 12 July 2011 10:53, Michael Wood <esiotrot@gmail.com> wrote:
> On 12 July 2011 09:31, Roy's Email <rmw256@hotmail.com> wrote:
>> Now I am particularly confused.  How do I issue "CREATE DATABASE ..." from
>
> Well, I've never tried it myself, but you can do it from "psql" and
> that connects to Postgres in order to do it.

Sorry if the above is not as clear as it could be.

To clarify, I do not mean that you should use psql to create the
database.  Only that psql does exactly the sort of thing you want to
be able to do, so it is possible :)

You only need to connect to one of the existing databases in order to
check for the existence of other databases or create a new one.  And
template0, template1 are predefined databases that should exist unless
something is wrong with the Postgres installation.

>> my program?  I thought that I needed a connection (PGconn) before I could
>> execute a command and I don't believe I have a valid connection if my
>> attempt to connect to a (non-existent) DB fails.  [Does it change anything
>
> That's why I said you should first connect to the "template1"
> database, which will exist.

--
Michael Wood <esiotrot@gmail.com>

Re: Creating DB programmatically?

От
Francisco Leovey
Дата:
I use a very simple perl program to get a list of databases and ask if you want a new one created and the desired encoding, and could add any other optional parameters.
The same is done in Webmin -

--- On Tue, 7/12/11, Jayadevan M <Jayadevan.Maymala@ibsplc.com> wrote:

From: Jayadevan M <Jayadevan.Maymala@ibsplc.com>
Subject: Re: [NOVICE] Creating DB programmatically?
To: "Roy's Email" <rmw256@hotmail.com>
Cc: pgsql-novice@postgresql.org, pgsql-novice-owner@postgresql.org
Date: Tuesday, July 12, 2011, 12:16 AM

Hello,
>
> I would like to create my DB programmatically.  I.e. If the DB does
> not already exist, I would like to be able to create (and populate)
> it from within my application.  Is there some way to do this?  
> Everything I see seems to require connecting to an existing DB
> before you can do anything.


If it is unix/linux, couldn't you do

[postgres@tcctest ~]$ psql -l | grep mydatabase
and then create one if it did not fetch any data?
If it exists, you will get something like
 mydatabase | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |

Regards,
Jayadevan





DISCLAIMER:


"The information in this e-mail and any attachment is intended only for the person to whom it is addressed and may contain confidential and/or privileged material. If you have received this e-mail in error, kindly contact the sender and destroy all copies of the original communication. IBS makes no warranty, express or implied, nor guarantees the accuracy, adequacy or completeness of the information contained in this email or any attachment and is not liable for any errors, defects, omissions, viruses or for resultant loss or damage, if any, direct or indirect."





Re: Creating DB programmatically?

От
Frank Bax
Дата:
On 07/12/11 03:31, Roy's Email wrote:
> Now I am particularly confused.  How do I issue "CREATE DATABASE ..."
> from my program? I thought that I needed a connection (PGconn) before I
> could execute a command and I don't believe I have a valid connection if
> my attempt to connect to a (non-existent) DB fails. [Does it change
> anything that I'm writing in C++ and using the 'libpq' interface?]



Let's assume for the moment; that you know how to connect to an existing
database and execute an SQL command like "SELECT ....".

Simply modify your code:
* change your connect command so it connects to "template1"
* change your "SELECT ..." statement to "CREATE DATABASE ..."
* close the connection and make a connection to database just created.



Re: Creating DB programmatically?

От
Pushpendra Singh Thakur
Дата:
some typing error plz read connect to database using maintenance database "postgres"

On 12 July 2011 20:00, Pushpendra Singh Thakur <thakur@corexprts.com> wrote:
Step 1 connect to user using maintenance db "ie postgres"
Step 2 check whether database exists or not "SELECT 1 AS result FROM pg_database 
WHERE datname='abc'"
Step 3 if not run "create database ...."

simple..


On 12 July 2011 17:14, Frank Bax <fbax@sympatico.ca> wrote:
On 07/12/11 03:31, Roy's Email wrote:
Now I am particularly confused.  How do I issue "CREATE DATABASE ..."
from my program? I thought that I needed a connection (PGconn) before I
could execute a command and I don't believe I have a valid connection if
my attempt to connect to a (non-existent) DB fails. [Does it change
anything that I'm writing in C++ and using the 'libpq' interface?]



Let's assume for the moment; that you know how to connect to an existing database and execute an SQL command like "SELECT ....".

Simply modify your code:
* change your connect command so it connects to "template1"
* change your "SELECT ..." statement to "CREATE DATABASE ..."
* close the connection and make a connection to database just created.



--
Sent via pgsql-novice mailing list (pgsql-novice@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice



--
Pushpendra Singh Thakur

COREEXPERTS Technologies Private Limited
Business Process Automation & IT Support Services
http://www.corexprts.com
Phone - 91-761-4070036
Fax - 91-761-4010530
SMS - 97-555-21174




--
Pushpendra Singh Thakur

COREEXPERTS Technologies Private Limited
Business Process Automation & IT Support Services
http://www.corexprts.com
Phone - 91-761-4070036
Fax - 91-761-4010530
SMS - 97-555-21174

Re: Creating DB programmatically?

От
Pushpendra Singh Thakur
Дата:
Step 1 connect to user using maintenance db "ie postgres"
Step 2 check whether database exists or not "SELECT 1 AS result FROM pg_database 
WHERE datname='abc'"
Step 3 if not run "create database ...."

simple..

On 12 July 2011 17:14, Frank Bax <fbax@sympatico.ca> wrote:
On 07/12/11 03:31, Roy's Email wrote:
Now I am particularly confused.  How do I issue "CREATE DATABASE ..."
from my program? I thought that I needed a connection (PGconn) before I
could execute a command and I don't believe I have a valid connection if
my attempt to connect to a (non-existent) DB fails. [Does it change
anything that I'm writing in C++ and using the 'libpq' interface?]



Let's assume for the moment; that you know how to connect to an existing database and execute an SQL command like "SELECT ....".

Simply modify your code:
* change your connect command so it connects to "template1"
* change your "SELECT ..." statement to "CREATE DATABASE ..."
* close the connection and make a connection to database just created.



--
Sent via pgsql-novice mailing list (pgsql-novice@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice



--
Pushpendra Singh Thakur

COREEXPERTS Technologies Private Limited
Business Process Automation & IT Support Services
http://www.corexprts.com
Phone - 91-761-4070036
Fax - 91-761-4010530
SMS - 97-555-21174