Обсуждение: Equivalent to "use database" in postgre

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

Equivalent to "use database" in postgre

От
javijava
Дата:
Hi,

i'm newby in postgre sql world.

i need to know how to do a simple script  that create a database,the y
select it (in other languajes using USE) and after create tables with this
database.


How can I say "use name_database" on postgre sql?

Thanks in advance 4 the help!!

--
View this message in context:
http://old.nabble.com/Equivalent-to-%22use-database%22-in-postgre-tp28990943p28990943.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


Re: Equivalent to "use database" in postgre

От
Scott Marlowe
Дата:
On Fri, Jun 25, 2010 at 7:10 AM, javijava <welove.e.music@gmail.com> wrote:
>
> Hi,
>
> i'm newby in postgre sql world.
>
> i need to know how to do a simple script  that create a database,the y
> select it (in other languajes using USE) and after create tables with this
> database.
>
>
> How can I say "use name_database" on postgre sql?

You have to re-connect to use a different db in pgsql.

Re: Equivalent to "use database" in postgre

От
Tim Landscheidt
Дата:
javijava <welove.e.music@gmail.com> wrote:

> i'm newby in postgre sql world.

> i need to know how to do a simple script  that create a database,the y
> select it (in other languajes using USE) and after create tables with this
> database.

> How can I say "use name_database" on postgre sql?

You must specify the database to use on connect; if you want
to use psql for your script, you can use "\c name_database"
à la:

| CREATE DATABASE testdatabase;
| \c testdatabase
| CREATE TABLE testtable (testcolumn int);

Tim

Re: Equivalent to "use database" in postgre

От
Tom Lane
Дата:
Scott Marlowe <scott.marlowe@gmail.com> writes:
> On Fri, Jun 25, 2010 at 7:10 AM, javijava <welove.e.music@gmail.com> wrote:
>> How can I say "use name_database" on postgre sql?

> You have to re-connect to use a different db in pgsql.

But keep in mind that mysql databases are more nearly akin to what
postgres calls schemas.  The best way to do what you want might be
to translate your mysql DBs into schemas, and then "set search_path"
would be a good approximation to "use".

            regards, tom lane

Re: Equivalent to "use database" in postgre

От
Ozz Nixon
Дата:
On Jun 25, 2010, at 10:48 AM, Scott Marlowe wrote:

> On Fri, Jun 25, 2010 at 7:10 AM, javijava <welove.e.music@gmail.com> wrote:
>>
>> Hi,
>>
>> i'm newby in postgre sql world.
>>
>> i need to know how to do a simple script  that create a database,the y
>> select it (in other languajes using USE) and after create tables with this
>> database.
>>
>>
>> How can I say "use name_database" on postgre sql?
>
> You have to re-connect to use a different db in pgsql.

And to elaborate a little more, the true need for "use database" for MySQL is not truly needed in most other RDBMS
solutions.In MySQL this allows you to use different database engines for different databases. It also allowed you to
keepyour project independent of others. Where more robust engines use schema and tablespace concepts to achieve this
foryou. 

So, I would not implement a re-connect theory, I would suggest researching the database(s) you plan on supporting and
seeingif there is a better way to leverage that engine for your needs. Which means, you need to know your needs - do
youneed data separation? Or, are you just used to sending the "use database" command? 

Ozz

Re: Equivalent to "use database" in postgre

От
Craig Ringer
Дата:
On 25/06/10 19:10, javijava wrote:
>
> Hi,
>
> i'm newby in postgre sql world.
>
> i need to know how to do a simple script  that create a database,the y
> select it (in other languajes using USE) and after create tables with this
> database.

http://wiki.postgresql.org/wiki/FAQ

http://www.coderholic.com/postgresql-for-mysql-users/

Most MySQL users misunderstand "databases" in postgresql. The closest
equivalent in PostgreSQL to a MySQL "database" is a PostgreSQL "schema".
If you expect to be able to run queries that use data from multiple
"databases" you really want to use schema.

See the help for the "psql" command for basic scripting, including the
"\c" command to connect to another DB. For help on an SQL command, run
"\h COMMANDNAME" in psql, or read the manual for that command.

--
Craig Ringer

Re: Equivalent to "use database" in postgre

От
Michael Nolan
Дата:


On Fri, Jun 25, 2010 at 10:00 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Scott Marlowe <scott.marlowe@gmail.com> writes:
> On Fri, Jun 25, 2010 at 7:10 AM, javijava <welove.e.music@gmail.com> wrote:
>> How can I say "use name_database" on postgre sql?

> You have to re-connect to use a different db in pgsql.

But keep in mind that mysql databases are more nearly akin to what
postgres calls schemas.  The best way to do what you want might be
to translate your mysql DBs into schemas, and then "set search_path"
would be a good approximation to "use".

The issue here may be data isolation.   Suppose you have two independent groups, one with database A and one with database B.  (Think of a company that hosts databases for its customers, for example.) 

In the ideal situation, the people with access only to database A should not even be able to learn that database B exists, much less access it in any fashion. 

I believe PG does not have the capability to completely isolate other databases from users running on the same server, I'm not sure if MySQL does or not.

However, if there are any times when it might be necessary to query both database A and database B, then schemas are the way to go. 
--
Mike Nolan
PG user by choice
MySQL user by necessity

Re: Equivalent to "use database" in postgre

От
John R Pierce
Дата:
On 06/25/10 4:10 AM, javijava wrote:
> i need to know how to do a simple script  that create a database,the y
> select it (in other languajes using USE) and after create tables with this
> database.
>
>
> How can I say "use name_database" on postgre sql?
>
> Thanks in advance 4 the help!!
>

IF you are using the psql command line utility to execute these scripts,
then
     \c dbname
will connect to a new database.  However, if you're processing these
scripts some other way, then your app will have to disconnect from the
one database and connect to the other itself (this is what the \c
command tells psql to do)