Is there a command for connecting to a database within an SQL file?
I want to create a database and then immediately begin populating it with tables and data. All contained within a single .sql file. I'm not aware of anything that replaces the \c shell command?
Example SQL file:
CREATE DATABASE mydb;
CREATE USER myusername WITH PASSWORD 'mypassword';
GRANT ALL PRIVILEGES ON DATABASE mydb TO myusername;
CONNECT TO mydb USER myusername; --<-- something like this?
CREATE TABLE administrator (
id integer NOT NULL,
admin_name character varying(150),
password character varying(255),
active boolean,
"timestamp" timestamp without time zone,
email character varying(255)
);
ALTER TABLE administrator OWNER TO myusername;
CONNECT is the closest thing I've found in the documentation, but it's encountering syntax errors. I couldn't find a wrapper that specifies where the tables should go either?
ERROR: syntax error at or near "CONNECT"
\cshould work provided you usepsqlto run that SQL script\cis a psql command, not a shell command. You can use it inside .sql scriptpsql's\c, then no, you cannot do that from inside the script.