I
I
input_denis2021-02-21 12:45:07
PostgreSQL
input_denis, 2021-02-21 12:45:07

How to specify in which database to create a table?

Hello.

To test the application, I want to use disposable containers with the postgres database. That is, run the container, conduct tests with saving the data in it, then delete the container and create the container again based on image.

To do this, I wrote init.sql with the following content:

CREATE USER parser WITH PASSWORD 'password';
CREATE DATABASE r;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO parser;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT INSERT ON TABLES TO parser;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT UPDATE ON TABLES TO parser;
CREATE table reviews (
    id integer NOT NULL PRIMARY KEY
);
COMMIT;


The container itself is launched via docker-compose
version: "3.1"

services:
  db_test:
    image: postgres
    restart: always
    environment:
      POSTGRES_PASSWORD: pass
    volumes:
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql
    ports:
    - "5444:5432"


The SQL script loads and executes without error, except for a warning that there are no transactions to commit.

I connect to the database created by the user without problems, but the table is created in the postgres database, and not in r, as we would like. I tried to google how to change the active database in SQL, but I did not find any solutions. Help me please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2021-02-21
@melkij

Disconnect from the currently connected base, connect to the desired one.
\connect dbname
if the file is passed through psql.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question