I'm trying to setup a postgres container with ansible, but the database isn't created completely. Via docker logs, it shows that the script has been executed entirely, but the result in always an empty database.
Playbook script:
- name: Run PostgreSQL container
become: no
docker_container:
env:
POSTGRES_PASSWORD: "{{ postgres_pw }}"
name: postgres
image: postgres:11
ports: "5432:5432"
restart_policy: unless-stopped
volumes:
- /tmp/{{ db_backup_file }}:/docker-entrypoint-initdb.d/{{ db_backup_file }}:ro
- pgdata:/var/lib/postgresql/data
sql file:
CREATE DATABASE database5;
--
-- TOC entry 647 (class 1247 OID 16548)
-- Name: geslacht; Type: TYPE; Schema: public; Owner: postgres
--
CREATE TYPE public.geslacht AS ENUM (
'm',
'v',
'x'
);
ALTER TYPE public.geslacht OWNER TO postgres;
SET default_tablespace = '';
--
-- TOC entry 197 (class 1259 OID 16555)
-- Name: antwoorden; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.antwoorden (
id integer NOT NULL,
bericht text NOT NULL,
gebruiker integer NOT NULL,
vraag integer NOT NULL,
gepost_op timestamp without time zone DEFAULT now() NOT NULL,
actief boolean DEFAULT true NOT NULL,
is_verkozen boolean DEFAULT false NOT NULL
);
Note: this is only a part of a script that creates all tables and adds sample data.
What's the reason why database5 is not being filled with the tables and data?
Related
I have docker-compose.yml
services:
postgres:
image: postgres:latest
container_name: postgres
ports:
- "5432:5432"
restart: unless-stopped
volumes:
- ./create-databases.sh:/docker-entrypoint-initdb.d/create-databases.sh
- ./db_persist:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: tenant_master
POSTGRES_DB2: tenant_1
POSTGRES_DB3: tenant_2
and File create-databases.sh
#!/bin/bash
set -eu
function create_database() {
local database=$1
echo " Creating database '$database'"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE DATABASE $database;
GRANT ALL PRIVILEGES ON DATABASE $database TO $POSTGRES_USER;
EOSQL
}
create_database $POSTGRES_DB2
create_database $POSTGRES_DB3
I need run below SQL snippet in database tenant_master (POSTGRES_DB)
CREATE TABLE public.tbl_tenant_master
(
tenant_client_id serial NOT NULL,
db_name character varying(128),
url character varying(512) NOT NULL,
user_name character varying(256) NOT NULL,
password character varying(256) NOT NULL,
driver_class character varying(512) NOT NULL,
status character varying(64),
PRIMARY KEY (tenant_client_id)
);
ALTER TABLE IF EXISTS public.tbl_tenant_master
OWNER to postgres;
(1)
use master_db;
INSERT INTO `master_db`.`tbl_tenant_master` (`tenant_client_id`, `db_name`, `url`, `user_name`, `password`, `driver_class`, `status`)
VALUES ('200', 'testdb', 'jdbc:mysql://127.0.0.1:3306/testdb?useSSL=false', 'root', 'root', 'com.mysql.cj.jdbc.Driver', 'Active');
SELECT * FROM master_db.tbl_tenant_master;
(2)
How to inject (1), then (2) to sh script (I mean create table then insert data by sh in Docker supplement)?
I have a docker-compose.yml file
version: '3'
services:
postgres:
image: postgres:13.1
healthcheck:
test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U", "root" ]
timeout: 45s
interval: 10s
retries: 10
restart: always
environment:
- POSTGRES_USER=root
- POSTGRES_PASSWORD=password
- APP_DB_USER=docker
- APP_DB_PASS=docker
- APP_DB_NAME=docker
volumes:
- ./db:/docker-entrypoint-initdb.d/
ports:
- 5432:5432
in the same directory there is a directory db with 1 file init.sql:
CREATE TABLE accounts (
user_id serial PRIMARY KEY,
username VARCHAR ( 50 ) UNIQUE NOT NULL,
password VARCHAR ( 50 ) NOT NULL,
email VARCHAR ( 255 ) UNIQUE NOT NULL,
created_on TIMESTAMP NOT NULL,
last_login TIMESTAMP
);
insert into accounts(username,password,email,created_on) values ('a','aaa','assdfdas',now())
when I run docker-compose up (if the db is empty) the init.sql file is executed but the database in which it is executed is root and not postgres, How can I change it?
Imgur link to DataGrip screenshot
You can use the psql command \connect <db-name> inside your init.sql file in order to connect to the correct database (given that the database already exists).
In your case, the init.sql file would look something like:
\connect postgres <-- connects to the 'postgres' database
CREATE TABLE accounts (
user_id serial PRIMARY KEY,
username VARCHAR ( 50 ) UNIQUE NOT NULL,
password VARCHAR ( 50 ) NOT NULL,
email VARCHAR ( 255 ) UNIQUE NOT NULL,
created_on TIMESTAMP NOT NULL,
last_login TIMESTAMP
);
...
my docker-compose:
version: '3.9'
services:
postgres:
image: postgres
restart: always
volumes:
- ./db:/docker-entrypoint-initdb.d
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: 123
POSTGRES_DB: location
ports:
- 5432:5432
/db/users.sql - init dump:
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
phone VARCHAR(255),
email VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TRIGGER users_moddatetime
BEFORE UPDATE ON users
FOR EACH ROW
EXECUTE PROCEDURE moddatetime (updated_at);
INSERT INTO users (first_name, last_name, phone, email) VALUES('Name', 'Lastname', '986754673823', 'test#mail.com');
INSERT INTO users (first_name, last_name, phone, email) VALUES('Test', 'TEst', '9878721632163', 'test2#mail.com');
When I'm runing docker-compose up I have an error function moddatetime() does
not exist. Any ideas?
After defining the table, the script generated by PGAdmin via rightclick->SCRIPT->CREATE SCRIPT, looks like this:
-- DROP TABLE public.arr;
CREATE TABLE public.arr
(
rcdno integer NOT NULL DEFAULT nextval('arr_rcdno_seq'::regclass),
rcdstate character varying(10) COLLATE pg_catalog."default" DEFAULT 'ACTIVE'::character varying,
rcdserial integer NOT NULL DEFAULT 0,
msrno integer NOT NULL DEFAULT nextval('arr_msrno_seq'::regclass),
mobileid character varying(50) COLLATE pg_catalog."default" NOT NULL,
edittimestamp timestamp without time zone DEFAULT now(),
editor character varying(20) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT pk_arr PRIMARY KEY (mobileid, msrno, rcdserial)
)
After using >pg_dump -s -U webmaster -W -F p Test > c:\temp\Test.sql, the table definition in the script OMITS the defaults for the 2 original "serial" columns. This means that the pg_dump script doesn't create the table correctly when it is run!
--
-- Name: arr; Type: TABLE; Schema: public; Owner: webmaster
--
CREATE TABLE public.arr (
rcdno integer NOT NULL, -- default missing!
rcdstate character varying(10) DEFAULT 'ACTIVE'::character varying,
rcdserial integer DEFAULT 0 NOT NULL,
msrno integer NOT NULL, -- default missing!
mobileid character varying(50) NOT NULL,
edittimestamp timestamp without time zone DEFAULT now(),
editor character varying(20) NOT NULL
);
ALTER TABLE public.arr OWNER TO webmaster;
--
-- Name: arr_msrno_seq; Type: SEQUENCE; Schema: public; Owner: webmaster
--
CREATE SEQUENCE public.arr_msrno_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.arr_msrno_seq OWNER TO webmaster;
--
-- Name: arr_msrno_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: webmaster
--
ALTER SEQUENCE public.arr_msrno_seq OWNED BY public.arr.msrno;
--
-- Name: arr_rcdno_seq; Type: SEQUENCE; Schema: public; Owner: webmaster
--
CREATE SEQUENCE public.arr_rcdno_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER TABLE public.arr_rcdno_seq OWNER TO webmaster;
--
-- Name: arr_rcdno_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: webmaster
--
ALTER SEQUENCE public.arr_rcdno_seq OWNED BY public.arr.rcdno;
EDIT: After the generated script has been run, the statement
into public.arr(mobileid, editor)
values
('12345', 'nolaspeaker')
generates
ERROR: null value in column "rcdno" violates not-null constraint DETAIL: Failing row contains (null, ACTIVE, 0, null, 12345, 2020-08-18 08:54:41.34052, nolaspeaker). SQL state: 23502
Which indicates that I am correct to assert that the script omits the default values for the rcdno column!
The ALTER TABLE statements that set the default values are towards the end of the dump, close to where the ALTER SEQUENCE ... OWNED BY are.
If they don't get restored, that might be because there was a problem creating the sequences.
Look at the error messages from the restore, particularly the first ones (later ones are often consequences of the earlier ones).
The statements that assign the default values to the applicable columns come much later in the script
--
-- Name: arr rcdno; Type: DEFAULT; Schema: public; Owner: webmaster
--
ALTER TABLE ONLY public.arr ALTER COLUMN rcdno SET DEFAULT nextval('public.arr_rcdno_seq'::regclass);
--
-- Name: arr msrno; Type: DEFAULT; Schema: public; Owner: webmaster
--
ALTER TABLE ONLY public.arr ALTER COLUMN msrno SET DEFAULT nextval('public.arr_msrno_seq'::regclass);
So you have to be careful if you cut-and-paste a table definition out of the script!
I want to pg_dump my database and have the problem that some sequences are generated in the dump file and some are not.
With the table infrastruktur_pictures it works with the table hoehenprofile it doesn't work. Here are the infos about the tables from pgadmin3:
hoehenprofile
CREATE SEQUENCE "hoehenprofile_HID_seq"
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 14289
CACHE 1;
ALTER TABLE "hoehenprofile_HID_seq";
CREATE TABLE hoehenprofile
(
"HID" serial NOT NULL,
hoehenprofil character varying(255),
CONSTRAINT "HID" PRIMARY KEY ("HID")
)
WITH (
OIDS=FALSE
);
ALTER TABLE hoehenprofile ADD COLUMN "HID" integer;
ALTER TABLE hoehenprofile ALTER COLUMN "HID" SET NOT NULL;
ALTER TABLE hoehenprofile ALTER COLUMN "HID" SET DEFAULT nextval('"hoehenprofile_HID_seq"'::regclass);
infrastruktur_pictures
CREATE SEQUENCE "infrastruktur_pictures_IPID_seq"
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
ALTER TABLE "infrastruktur_pictures_IPID_seq";
CREATE TABLE infrastruktur_pictures
(
"IPID" serial NOT NULL,
picture character varying(255) NOT NULL,
picture_text text,
fotograf text,
CONSTRAINT prim PRIMARY KEY ("IPID")
)
WITH (
OIDS=FALSE
);
ALTER TABLE infrastruktur_pictures ADD COLUMN "IPID" integer;
ALTER TABLE infrastruktur_pictures ALTER COLUMN "IPID" SET NOT NULL;
ALTER TABLE infrastruktur_pictures ALTER COLUMN "IPID" SET DEFAULT nextval('"infrastruktur_pictures_IPID_seq"'::regclass);
In the dump file the generated Code for hoehenprofile looks like this(no sequence is generated):
--
-- TOC entry 145 (class 1259 OID 67719)
-- Name: hoehenprofile; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE hoehenprofile (
"HID" integer DEFAULT nextval('"hoehenprofile_HID_seq"'::regclass) NOT NULL,
hoehenprofil character varying(255)
);
The code for infrastruktur_pictures looks like this:
-
-- TOC entry 152 (class 1259 OID 67750)
-- Name: infrastruktur_pictures; Type: TABLE; Schema: public; Owner: -; Tablespace:
--
CREATE TABLE infrastruktur_pictures (
"IPID" integer NOT NULL,
picture character varying(255) NOT NULL,
picture_text text,
fotograf text
);
--
-- TOC entry 4230 (class 0 OID 0)
-- Dependencies: 152
-- Name: TABLE infrastruktur_pictures; Type: COMMENT; Schema: public; Owner: -
--
COMMENT ON TABLE infrastruktur_pictures IS 'Bilder im Infrastruktur Bereich des Backends';
--
-- TOC entry 153 (class 1259 OID 67756)
-- Name: infrastruktur_pictures_IPID_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE "infrastruktur_pictures_IPID_seq"
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- TOC entry 4231 (class 0 OID 0)
-- Dependencies: 153
-- Name: infrastruktur_pictures_IPID_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE "infrastruktur_pictures_IPID_seq" OWNED BY infrastruktur_pictures."IPID";
The only difference between this two tables i can see in pgadmin3. When i click on the specific cloumn(HID, IPID) in the object browser and look at the properties Tab right of it i can see that at the HID column the sequence attribute is not set.
The difference is that in the second case sequence is owned by table. Check last line of pg_dump.
This is because, when you create table using serial pseudo-datatype, the ownership is automatically added. But when you created sequence manually, you didn't set "OWNED BY".