Skip to content

Instantly share code, notes, and snippets.

@Alex-Just
Last active August 13, 2018 12:39
Show Gist options
  • Save Alex-Just/a7cc65b8deb3a689b9c4b70f4286109f to your computer and use it in GitHub Desktop.
Save Alex-Just/a7cc65b8deb3a689b9c4b70f4286109f to your computer and use it in GitHub Desktop.
# Login as a UNIX user (IDENT/PEER authentication)
sudo -u postgres psql postgres
# Login via PostgreSQL's own managed username/password (TCP authentication)
psql username -h 127.0.0.1 -d dbname
# Switch to postgres user via root
sudo -i -u postgres
# Backup DB
pg_dump dbname > dbname.sql
psql dbname < dbname.sql
pg_dump -Fc test_dbname > /tmp/test_dbname
psql dbname < /tmp/test_dbname.sql
pg_restore /tmp/test_dbname -d dbname -e -c
pg_dump -C -h 127.0.0.1 -U username dbname | psql -h localhost -U username dbname
pg_dump -t dbname_tasks dbname | psql -d dbname
psql -c "copy (select * from dbname_task ) to stdin " dbname2 | psql -c "copy dbname_task2 from stdout" dbname
# KILL ALL EXISTING CONNECTION FROM ORIGINAL DB
psql -c "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'dbname' AND pid <> pg_backend_pid();"
# switch db
dropdb dbname
createdb -O dbnameuser -T test_dbname dbname
# dump dbname
pg_dump dbname > dump.sql
pg_dumpall -U postgres > /tmp/dump2.sql
scp username@host:/var/lib/postgresql/dump.sql ~/Desktop/dump.sql
pg_dump dbname < ~/Desktop/dump.sql
psql dbname < ~/Desktop/dump.sql
# Count distinct values
SELECT COUNT(*) FROM (SELECT DISTINCT col FROM table) AS q
# Select duplicates
SELECT col, count(*) FROM table GROUP BY col HAVING count(*) > 1;
# Deleting duplicates
DELETE FROM tablename
WHERE id IN (SELECT id
FROM (SELECT id,
ROW_NUMBER() OVER (partition BY column1, column2, column3 ORDER BY id) AS rnum
FROM tablename) t
WHERE t.rnum > 1);
# How to search a specific value everywhere
http://stackoverflow.com/a/5351627/1334996
pg_dump --data-only --inserts -U postgres DATABASE > /tmp/dump.sql
grep TEXTTOSEARCH /tmp/dump.sql
# Add id column
ALTER TABLE test1 ADD COLUMN id SERIAL PRIMARY KEY;
# list all databases
\l
# list all tables
\dt
# list all columns
SELECT * FROM information_schema.columns WHERE table_schema = 'your_schema' AND table_name = 'your_table';
# Fix serial field
SELECT setval(pg_get_serial_sequence('table', 'id'), coalesce(max(id),0) + 1, false) FROM table;
# show index from [table]
select * from pg_indexes where tablename = 'your_table';
# Copy table's structure
CREATE TABLE new_table ( LIKE old_table INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING INDEXES);
# Query to a file
psql -c "COPY (<select query>) TO STDOUT WITH CSV HEADER"
psql -c "COPY (SELECT * FROM dbname.dbtable LIMIT 1000000) TO STDOUT WITH CSV HEADER" > /tmp/dbname.dbtable.sql
`psql -d main_data -c "COPY (SELECT * FROM dbname.dbtable) TO STDOUT WITH CSV HEADER" | gzip > /tmp/dbname.dbtable.sql.gz`
# Find a “gap” in running counter
SELECT id + 1
FROM table s
WHERE NOT EXISTS
(
SELECT NULL
FROM table t
WHERE t.id = s.id + 1
)
ORDER BY id;
# List of tables' indexes
select conrelid::regclass AS table_from, conname, pg_get_constraintdef(c.oid)
from pg_constraint c
join pg_namespace n ON n.oid = c.connamespace
where contype in ('f', 'p','c','u') order by contype
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment