postgres

Dies ist eine alte Version des Dokuments!


postgres

Shell Zugang

thommie@db2b:~$ sudo su postgres
postgres@db2b:/home/thommie$ psql
psql (13.6 (Ubuntu 13.6-1.pgdg20.04+1))
Type "help" for help.
postgres=# 

\l listet alle DBs

Zu einer DB verbinden

postgres=# \c openproject postgres
You are now connected to database "openproject" as user "postgres".

\dt listet alle Tabellen

Schemata: https://www.postgresqltutorial.com/postgresql-administration/postgresql-schema/ Das Standard Schema ist public

Alle Tabellen löschen

DO $$ DECLARE
  r RECORD;
BEGIN
  FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = current_schema()) LOOP
    EXECUTE 'DROP TABLE ' || quote_ident(r.tablename) || ' CASCADE';
  END LOOP;
END $$;

\s command history

\q DB shell beenden

Postgres und UTF8

Standardmässig werden neue Datenbanken mit Zeichenkondierung SQL_ASCII angelegt. Das passt meistens, aber nicht immer. Mit dieser methode werden DBs mit UTF8 angelegt: https://www.shubhamdipt.com/blog/how-to-change-postgresql-database-encoding-to-utf8/

$ pg_dump your_database > dump.sql​

Datenbank löschen

$ dropdb your_database​
postgres=# UPDATE pg_database SET datistemplate = FALSE WHERE datname = 'template1';
postgres=# DROP DATABASE template1;
postgres=# CREATE DATABASE template1 WITH TEMPLATE = template0 ENCODING = 'UTF8';
postgres=# UPDATE pg_database SET datistemplate = TRUE WHERE datname = 'template1';
postgres=# \c template1;
You are now connected to database "template1" as user "postgres".
template1=# VACUUM FREEZE;

Neu angelegte DBs auf Basis von template1 haben danach utf8 encoding

  • postgres.1652888187.txt.gz
  • Zuletzt geändert: 05/03/2024 - 10:52
  • (Externe Bearbeitung)