Last active
May 20, 2020 12:57
-
-
Save denitram/9249274 to your computer and use it in GitHub Desktop.
Potsgresql: handy commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Viewing current PostgreSQL queries | |
-- source: http://chrismiles.info/systemsadmin/databases/articles/viewing-current-postgresql-queries/ | |
SELECT datname,pid,query,query_start FROM pg_stat_activity ORDER BY query_start ASC; | |
-- top-like view of current queries, grouped by how many of the same query are running at that instant and the usernames belonging to each connection. | |
SELECT count(*) as cnt, usename, current_query FROM pg_stat_activity GROUP BY usename,current_query ORDER BY cnt DESC; | |
-- List databases creation dates | |
SELECT (pg_stat_file('base/'||oid ||'/PG_VERSION')).modification, datname FROM pg_database ORDER BY modification; | |
SELECT (pg_stat_file('base/'||oid ||'/PG_VERSION')).modification, datname FROM pg_database ORDER BY datname; | |
-- List tables in a particular schema | |
\dt schemaname.* | |
-- Change postgres password | |
$ sudo -u postgres psql postgres | |
postgres=# \password postgres | |
-- Export a table to a csv file | |
COPY tablename TO '/tmp/filename.csv' HEADER CSV; | |
-- Import a csv file with null values integer | |
COPY public."tablename" FROM '/path/file.csv' USING DELIMITERS ',' csv NULL AS ''; | |
-- Drop all tables | |
SELECT 'DROP table IF EXISTS "' || tablename || '" CASCADE;' | |
FROM pg_tables | |
WHERE schemaname = 'public'; | |
-------------------------------------- | |
-- drop table if exists "tab1" cascade; | |
-- drop table if exists "tab2" cascade; | |
-- (2 rows) | |
-- Modify owner (getest) | |
SELECT format( | |
'ALTER TABLE %I.%I.%I OWNER TO %I;', | |
table_catalog, | |
table_schema, | |
table_name, | |
current_user -- or another just put it in quotes | |
) | |
FROM information_schema.tables | |
WHERE table_schema = 'mySchema'; | |
In psql, you can run them by following it immediately with \gexec | |
-- Modify owner on all tables | |
for tbl in `psql -Upostgres -hlocalhost -qAt -c "select tablename from pg_tables where schemaname = 'public';" YOUR_DB` ; do psql -Upostgres -hlocalhost -c "alter table $tbl owner to NEW_OWNER" YOUR_DB ; done | |
-- Modify owner on all sequences | |
for tbl in `psql -Upostgres -hlocalhost -qAt -c "select sequence_name from information_schema.sequences where sequence_schema = 'public';" YOUR_DB` ; do psql -Upostgres -hlocalhost -c "alter table $tbl owner to NEW_OWNER" YOUR_DB ; done | |
-- zie https://gist.github.com/gingerlime/2482969 - change_db_owner.sh | |
-- Listing the number of records in all postgresql tables | |
(source: http://linfiniti.com/2011/06/listing-the-number-of-records-in-all-postgresql-tables) | |
$ for TABLE in $(psql foo-test -c "\dt" | awk '{print $3}') | |
do | |
psql foo-test -c "select '$TABLE', count(*) from $TABLE;" | grep -A1 "\-\-\-\-" | tail -1 | |
done | |
-- change owner | |
REASSIGN OWNED BY old_role [, ...] TO new_role | |
# create read-only user | |
# source: https://blog.ed.gs/2016/01/12/add-read-only-postgres-user/ | |
psql databasehere | |
CREATE USER moodle_readonly WITH ENCRYPTED PASSWORD 'blablablapasswordhere'; | |
GRANT CONNECT ON DATABASE moodle TO moodle_readonly; | |
GRANT USAGE ON SCHEMA public TO moodle_readonly; | |
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO moodle_readonly; | |
GRANT SELECT ON ALL TABLES IN SCHEMA public to moodle_readonly; | |
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO moodle_readonly; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment