siehe auch: PostgreSQL
> pg_dump --schema-only \ -t tabelle1 -t tabelle2 -t tabelle3 -t tabelle4 -t tabelle5 \ -U postgres -d database_name \ -f schema_5_tabelles.sql
> pg_dump --schema-only --section=pre-data \ -t tabelle1 -t tabelle2 -t tabelle3 -t tabelle4 -t tabelle5 \ -U postgres -d database_name \ -f create_5_tabelles_only.sql
> pg_dump --schema-only --no-owner --no-privileges \ -t tabelle1 -t tabelle2 -t tabelle3 -t tabelle4 -t tabelle5 \ -U postgres -d database_name \ -f create_5_tabelles_no_owner.sql
> psql -U "$USER" -d "$TARGET_DB" -f schema_5_tabelles.sql
Was PostgreSQL 15 (Released: Oktober 2022) neu bietet HEADER MATCH für den COPY-Befehl, beim einspielen von Daten – automatische Spalten-Validierung.
Die Tabellen müssen bereits existieren!
echo "COPY tabelle TO 'export_tabelle.tsv' WITH (FORMAT csv, DELIMITER E'\t', HEADER true)" | sudo -u postgres psql -d database_name
Ohne Header (mit -t) COPY tabelle FROM 'file.tsv' WITH (FORMAT csv, DELIMITER E'\t'); COPY tabelle FROM 'file.tsv' WITH (FORMAT csv, DELIMITER E'\t', HEADER false); Mit Header (ohne -t) - Wenn du nur Spaltennamen überspringen willst COPY tabelle FROM 'file.tsv' WITH (FORMAT csv, DELIMITER E'\t', HEADER); COPY tabelle FROM 'file.tsv' WITH (FORMAT csv, DELIMITER E'\t', HEADER true); Mit Header (ohne -t) - Wenn du validieren willst, dass Header-Namen passen COPY tabelle FROM 'file.tsv' WITH (FORMAT csv, DELIMITER E'\t', HEADER MATCH);
echo "COPY tabelle FROM 'export_${TAB}.tsv' WITH (FORMAT csv, DELIMITER E'\t', HEADER MATCH);" | sudo -u postgres psql -d database_name
ssh dbuser@hostname "echo \"SELECT * FROM tabelle;\" | psql -d testdb -U dbuser -A -F $'\t'" | psql -d testdb -U dbuser -c "COPY tabelle FROM STDIN WITH (FORMAT csv, DELIMITER E'\t', HEADER MATCH);" oder ssh dbuser@hostname "echo \"SELECT * FROM tabelle;\" | psql -d testdb -U dbuser -A -F $'\t'" | psql -d testdb -U dbuser -c "COPY tabelle FROM STDIN WITH (FORMAT csv, DELIMITER E'\t', HEADER MATCH, NULL '');"
echo "DROP TABLE tabelle;" | sudo -u postgres psql -d testdb
cat Test-Tabellen-Schema.sql | psql -d testdb -U dbuser echo "\dt" | sudo -u postgres psql -d testdb
(cat header.txt | tr -s '\n' '\t' | sed 's/\t$//'; echo ; cat export.tsv) > export+header.tsv
echo "COPY tabelle1 FROM 'export+header.tsv' WITH (FORMAT csv, DELIMITER E'\t', HEADER MATCH, NULL '');" | psql -U dbuser -d testdb oder cat export+header.tsv | psql -d testdb -U dbuser -c "COPY tabelle FROM STDIN WITH (FORMAT csv, DELIMITER E'\t', HEADER MATCH, NULL '');"
echo "SELECT COUNT(*) AS tabelle FROM tabelle;" | psql -d testdb -U dbuser