Create a database in sql*plus (sql command line) - sql

I've used mysql shell for creating database (create database testdb), why can't i make a database in sql*plus command line. I also want to see the lists of database but i can't find queries anywhere. Please guide
SQL> create database testdb;
create database testdb
*
ERROR at line 1:
ORA-01501: CREATE DATABASE failed
ORA-01100: database already mounted
SQL>

Related

Unable to drop database in PSQL

I am unable to delete a database called postgres. Here is my code from trying to drop it in the command line in Git-Bash:
default=# DROP DATABASE postgres;
ERROR: database "postgres" is being accessed by other users
DETAIL: There are 2 other sessions using the database.
I am the only person using the database. Also when I right click on the postgres database in pgAdmin, the delete/drop option does not appear. Any help is appreciated!

How to connect from one schema to another schema of same database automatically when running a .sql file?

I am running a test.sql file from command prompt by connecting to the database named DB. The test.sql file contains DDL, DML statments in it.
The Schemas named A and B are used in the test.sql file.
First connection to the schema connect&&schema A was made. The DDL, DML statements for schema A got executed.
Next connect&&schema B was made in that same test.sql file. So the script at run time asks for the value of schema name B. After the connection to schema B then the DDL, DML statements for schema B will be executed.
Please suggest if I can connect automatically to schema B without entering the value of schema B name at run time when running the test.sql file.

PostgreSQL create a schema under a different database using a script

So what I want to do here is to run a script while connected to a database I already had using pgAdmin3. The script contains a create role, tablespace, database and a create schema and several tables under that schema.
The problem here is that when I run the script it creates the new role, tablespace and database correctly. It also creates the schema and the tables correctly but with a problem, the schema is created under the database, from which I ran the script, instead of the newly created database. The script is more or less like this.
CREATE ROLE "new_role" ... ;
CREATE TABLESPACE "new_space"
OWNER "new_role"
LOCATION '/home/...';
CREATE DATABASE "new_db"
WITH OWNER = "new_role"
TABLESPACE = "new_space";
CREATE SCHEMA "schema" AUTHORIZATION "new_role" ;
CREATE TABLE IF NOT EXISTS "schema"."new_table"(
...
) TABLESPACE "new_space";...
...
I already saw a solution with a \connect foo; but that is not what I wanted, I wanted it to somehow connect within the script without running things separately and running \connect foo in the terminal.
Can anyone tell me if there is anyway to do this and help me come out with a solution to this problem?
Use psql and split it up into two scripts . You can save the scripts in .sql files, and then run psql to connect to the DB you want to run each script against all on the same command line (with && in between each command). The two psql commands could be combined into one bash script so it's only one command that you need to run.
Something like this, if the script were named foo.sql:
psql -X -h <host> -U <user> -p <port> -f foo.sql <db_name>
The first script could have the create role, create tablespace and create database commands, connecting to the postgres db or a template DB, and the second script could have the rest of the commands.
You could also use createdb from the bash script instead of CREATE DATABASE.
Using pgAdminIV:
1- right click on default database "postgres"
2- select create database, give a name f.e. "newdatabase"
3- click on "newdatabase" (to establish connection)
4- open the query tool
5- import, write or paste your code
6- run your code f.e.: CREATE SCHEMA newschema;
It works for me...

CREATE and DROP database won't work [duplicate]

This question already has answers here:
In psql, why do some commands have no effect?
(2 answers)
Closed 9 years ago.
I want simply drop some databases and after that create a new one.
Within postgresql version 9.1, running these commands first to create:
postgres=# createdb [dbname]
or
postgres=# CREATE DATABASE name
as described here Postgresql Documentation.
Now, to drop away some databases:
postgres=# DROP DATABASE name
as described here as well Postgresql Documentation.
They all didn't work. What am I missing?
You forgot the semicolons.
postgres=# DROP DATABASE name;
SQL commands may carry on over multiple lines, and are only sent to the server when you end them with a semicolon. That's why the prompt changes:
postgres=# DROP DATABASE name
postgres-#
It might be a good idea to take a look through the tutorial.
Additionally createdb isn't an SQL command. It's a shell utility command that wraps CREATE DATABASE for convenience.
See also:
Can't delete database
In psql, why do some commands have no effect?
I think you have to be a superuser because accroding to the documentation :
for create a new database :
To create a database, you must be a superuser or have the special
CREATEDB privilege. See CREATE USER.
for drop a database
DROP DATABASE drops a database. It removes the catalog entries for the
database and deletes the directory containing the data. It can only be
executed by the database owner.
Read this tutorial, it will help you.

PostgreSQL: Create schema in specific database

I need to write an sql script that creates both a new database AND a new schema in the database I just created.
How can I do it? Can I somehow change the current database to the new one? Or can I somehow specify the database for CREATE SCHEMA?
I'm using PostgreSQL 9.0
You can connect to the database, and execute the "CREATE SCHEMA" statement. That should result in a new schema in that database. It's not as tough as you think ;) When you want to do this from a .SQL file instead, you can use the \connect command as such:
CREATE DATABASE foo;
\connect foo;
CREATE SCHEMA yourschema;
Login to New-Database with new user:
postgres=> \connect newdb user1
...
You are now connected to database "newdb" as user "user1".
newdb=>
To create schema with new user "user1" in newdb:
newdb=> CREATE SCHEMA s1;
To list the schema :
SELECT * from information_schema.schemata;
Create database using
--CREATE DATABASE test;
Enter to the test database using
--psql -d test;
Create your schema in test database using
--create schema if not exists test_schema;