In Postgres, PSQL one can execute commands from file with the \i <path>
command.
I did this by creating a DATABASE, a SCHEMA, some TABLES, and a VIEW with this little file:
DROP DATABASE IF EXISTS databaseName;
CREATE DATABASE databaseName ;
CREATE SCHEMA IF NOT EXISTS databaseName_schema;
CREATE TABLE IF NOT EXISTS databaseName_schema.tableName (
id SERIAL PRIMARY KEY,
name VARCHAR(80),
surename VARCHAR(80),
);
CREATE TABLE IF NOT EXISTS databaseName_schema.tableMovies (
id SERIAL PRIMARY KEY,
priority SERIAL,
movieName VARCHAR(40)
);
create or replace view versionViewTest as select version();
I then checked with \l and the database "databaseName" does exist. If I then
DROP DATABASE databaseName;
and recheck \l the database is gone! However when I then execute the same script again with
\i <path>
it says:
...schema already exists, skipping CREATE SCHEMA ...relation already exists, skipping CREATE TABLE
I already spent some time reading about schemas, relations, and Postgres and can not tell what I am missing.
My expectation would be: If I drop a database it's containing components also are dropped but somehow psql does not drop them but reuses them in case of the creation of a database with the exact same name in the future. Why would that be useful? And how would I drop the database with all its content?
PS: Any lecture on that topic is welcomed as well as a direct answer.
you forgot to connect to a db:
DROP DATABASE IF EXISTS databaseName;
CREATE DATABASE databaseName ;
\c databaseName
CREATE SCHEMA IF NOT EXISTS databaseName_schema;
...