sh postgres.sh

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh postgres.sh相关的知识,希望对你有一定的参考价值。

# 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

以上是关于sh postgres.sh的主要内容,如果未能解决你的问题,请参考以下文章

如何使我的命令行在具有扩展名(.sh)和名称如“weird.sh.sh.sh”的文件上工作

sh sh_template.sh

sh sh.sh

Linux下面如何运行 SH文件

配置告警系统主脚本main.sh mon.sh load.sh 502.sh disk.sh

shell 脚本各种执行方式(source ./*.sh, . ./*.sh, ./*.sh)的区别