Skip to content

Instantly share code, notes, and snippets.

@hvardhanx
Last active May 1, 2018 11:13
Show Gist options
  • Save hvardhanx/74b4c220870393cb431397984a10ea5e to your computer and use it in GitHub Desktop.
Save hvardhanx/74b4c220870393cb431397984a10ea5e to your computer and use it in GitHub Desktop.

Binary data to SQL

pg_restore db.bin > db.sql

Log in to postgres

sudo -u postgres -i

Import from .sql file into Postgresql

psql -U postgres -W -d "valuevest" -f vv.sql -h localhost

Export a column to CSV:

Copy from CSV to postgres table

  • Move the file to /tmp. Run: copy <table_name> FROM '/tmp/<filename>.csv' WITH (FORMAT csv);

Single table dump:

$ pg_dump --column-inserts --data-only --table=<table> <database>
$ pg_dump -h localhost -p 5432 -U postgres -d mydb -t my_table > backup.sql

Show columns in a table:

postgres=# SELECT * FROM information_schema.columns WHERE table_schema = 'your_schema' AND table_name   = 'your_table'
postgres=# \d+ <table_name>

Resync primary_key field:

SELECT setval('tablename_id_seq', (SELECT MAX(id) FROM tablename)+1)

Expanded display:

\x on

Delete older data from database:

TableName.objects.filter(date__gte=datetime.now()-timedelta(days=14)).delete()

Reference:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment