psql

postgresql - get a list of columns difference between 2 tables

大憨熊 提交于 2019-12-11 11:12:26
问题 We are using Redshift which is using Postgres 8. I need to compare (2) tables which will almost be identical, but the other table will have extra columns so, I need to find out the column difference. Example: CREATE TABLE table1 ( v_id character varying(255) NOT NULL, v_created timestamp without time zone NOT NULL, abc_102 boolean, abc_103 boolean, abc_104 boolean, def_56 boolean DEFAULT false NOT NULL, def_57 boolean DEFAULT false NOT NULL ) CREATE TABLE table2 ( v_id character varying(255)

Passing argument to a psql procedural script

耗尽温柔 提交于 2019-12-11 11:05:35
问题 I have a loop command script on psql that looks like this: script.sql DO $$DECLARE data_rec RECORD; r RECORD; r2 RECORD; BEGIN select mytables.data_id into data_rec from mytables where id = :arg1; FOR r IN select * from (select * from ...) LOOP FOR r2 IN select * from (...) LOOP ...... END LOOP; END LOOP; END$$; And I want to pass arg1 as a argument from the command line: psql -h db.server.com -f script.sql -v arg1=1234 > foo.out But I keep getting a syntax error at where id = :arg1 , so I'm

Getting error while syncdb django.db.utils.ProgrammingError: permission denied for relation django_migrations

≡放荡痞女 提交于 2019-12-11 10:46:39
问题 I have installed postgreSQL and psycopg2, then I created a db and user, I have granted all the permission for the user to the db and I have made all the changes in settings.py . But while I try to migrate, I am getting the error django.db.utils.ProgrammingError: permission denied for relation django_migrations settings.py : DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'easyapp', 'USER': 'krishna', 'PASSWORD': 'sudeep', 'HOST': 'localhost', 'PORT': '',

How to save and restore value of ON_ERROR_STOP?

房东的猫 提交于 2019-12-11 09:55:59
问题 Is there a way to save, temporarily change, and then restore the value of the psql ON_ERROR_STOP variable? Basically, I'd like to have the "moral equivalent" of the following in a psql script: save_on_error_stop=ON_ERROR_STOP \unset ON_ERROR_STOP ALTER TABLE foo DROP COLUMN bar; -- (for example) \set ON_ERROR_STOP save_on_error_stop ALTER TABLE foo ADD COLUMN bar; The point being that the '\set' command at the end won't actually set ON_ERROR_STOP unless it was set before. 回答1: I don't think

create database using psql in shell script takes username as db name

淺唱寂寞╮ 提交于 2019-12-11 08:14:13
问题 Following is the command I am using to create database in shell script psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" \ -c "CREATE DATABASE $database;" It throws error FATAL: database "POSTGRES_USER" does not exist If I print $database and $POSTGRES_USER they show correct values but when this command runs with some reason the username is passed as the database name. any clue what I might be missing? ====== function create_db() { local database=$1 local query_databases="select datname

Benchmarking many PostgreSQL queries

六月ゝ 毕业季﹏ 提交于 2019-12-11 08:03:50
问题 On a complex webpage more than hundred queries are executed. I search for a way to benchmark these queries. I tried to embed EXPLAIN ANALYZE into a query resp. into a psql function, to pick the execution time of every query and compare them. But EXPLAIN ANALYZE seems not to be embeddable in any way. Is there another solution to compare the execution time of some queries? Thanks a lot 回答1: There is no problem with embedding EXPLAIN in PL/pgSQL: CREATE OR REPLACE FUNCTION profile( IN query text

Django ORM exclude fails

点点圈 提交于 2019-12-11 07:08:25
问题 I have some problems with my query - with filter() it's ok but with exclude() doesn't work. My models: class Dictionary(DateTimeModel): base_word = models.ForeignKey(BaseDictionary, related_name=_('dict_words')) word = models.CharField(max_length=64) version = models.ForeignKey(Version) class FrequencyData(DateTimeModel): word = models.ForeignKey(Dictionary, related_name=_('frequency_data')) count = models.BigIntegerField(null=True, blank=True) source = models.ForeignKey(Source, related_name=

ActiveRecord::StatementInvalid: PG::Error: ERROR: must be owner of database

妖精的绣舞 提交于 2019-12-11 06:59:07
问题 I'm new to rails , Some errors occurred when I tried to run bundle exec rake test:prepare , Same question had been asked Click here ,I have included the psql output. Anyone can help? Thank you. I got flowing errors : rake aborted! ActiveRecord::StatementInvalid: PG::Error: ERROR: must be owner of database testp00 : DROP DATABASE IF EXISTS "testp00" /Users/snailwalker/.rvm/gems/ruby-2.0.0-p481/gems/activerecord-4.0.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:128

How to programmatically find out which queries the psql command “\d+” does

旧城冷巷雨未停 提交于 2019-12-11 06:27:51
问题 I'm looking for a solution to generate postgreSQL database statistics programmatically. The psql commmand "\d+" is exactly what I need (output-wise), but so far I didn't find a way to retrieve this same data programmatically (by using C# and npgsql.dll). Any suggestions? Thanks in advance! 回答1: The answer is here: http://wiki.postgresql.org/wiki/Disk_Usage I find it useful to have a function: https://gist.github.com/ArtemGr/8434901 See also: PostgreSQL "DESCRIBE TABLE" 回答2: Apparently the

Ubuntu psql: could not connect to server

断了今生、忘了曾经 提交于 2019-12-11 04:40:43
问题 I have a local installation of postgresql. It does not start, as I get: psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"? Starting it manually does not work: sudo /etc/init.d/postgresql restart * Restarting PostgreSQL 9.4 database server * The PostgreSQL server failed to start. Please check the log output: 2015-03-09 17:41:39 CET [3769-1] FATAL: could not create lock