postgresql Postgres SQL文件中的CONNECT

nue99wik  于 2022-12-23  发布在  PostgreSQL
关注(0)|答案(1)|浏览(145)

是否有命令用于连接到SQL文件 * 内 * 的数据库?
我想创建一个数据库,然后立即开始用表和数据填充它。所有这些都包含在一个.sql文件中。我不知道有什么东西可以取代\c shell命令?
示例SQL文件:

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是我在文档中找到的最接近的东西,但是它遇到了语法错误。我也找不到一个指定表应该放在哪里的 Package 器。
ERROR: syntax error at or near "CONNECT"

thtygnil

thtygnil1#

添加以下语句代替connect to db

\c mydb;

执行的重新排序语句应为

CREATE DATABASE mydb;
\c mydb;
CREATE USER myusername WITH PASSWORD 'mypassword';
GRANT ALL PRIVILEGES ON DATABASE mydb TO myusername;

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;

相关问题