psql
how to execute .sql files in postgres database
Use the psql command line tool: psql -f file_with_sql.sql This command executes all commands line-by-line (except when the file contains BEGIN…END blocks. In this case, commands in blocks execute in a transaction). To wrap all commands in a transaction use the –single-transaction switch: psql –single-transaction -f file_with_sql.sql For more options: psql –help
foreigner – remove foreign key
# Removes the given foreign key from the table. # Removes the foreign key on +accounts.branch_id+. remove_foreign_key :accounts, :branches # Removes the foreign key on +accounts.owner_id+. remove_foreign_key :accounts, column: :owner_id # Removes the foreign key named +special_fk_name+ on the +accounts+ table. remove_foreign_key :accounts, name: :special_fk_name Offical doc: http://api.rubyonrails.org/v4.2/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-remove_foreign_key
How to set default display mode in psql
Use .psqlrc to set defaults. For the specific case of \x, newer psql versions (9.3, I think, but it might just be the 9.4 pre-release) can automatically switch to expanded output mode when the rows are too wide to fit on a line. From \?: \x [on|off|auto] toggle expanded output (currently off) So I suggest … Read more
will pg_restore overwrite the existing tables?
If you use the –clean option of pg_restore, the old tables will be dropped before the new ones are created. If you do not use the –clean option, you will get an error message that the table already exists, but pg_restore will continue processing unless you use the –exit-on-error option. If your goal is to … Read more
PostgreSQL error: could not connect to database template1: could not connect to server: No such file or directory
The error means that the Postgres server is not running. Try starting it: sudo systemctl start postgresql I believe the service name is postgresql, but if that doesn’t work, try typing sudo systemctl start postgres and pressing tab to auto-complete. Make sure that the server starts on boot: sudo systemctl enable postgresql
Is there a “pg_restore –quiet” option like “psql –quiet”?
The question seems to imply that pg_restore is executing these SQL commands and you wouldn’t want to see them in the output. But outputting them is what it’s only supposed to do. pg_restore has two modes of operation, with or without connecting to a database. When it’s called without a database (-d option) as shown … Read more
PostgreSQL user listing
User aren’t actually, “for the database”, they’re for cluster and are given different permissions to access databases. To list users \du should do, but you need to be connected. Something like psql template1 -c ‘\du’ from the command line prompt should do. (or \du from psql prompt when you are connected to the database).
How can I connect to a postgreSQL database into Apache Spark using scala?
Our goal is to run parallel SQL queries from the Spark workers. Build setup Add the connector and JDBC to the libraryDependencies in build.sbt. I’ve only tried this with MySQL, so I’ll use that in my examples, but Postgres should be much the same. libraryDependencies ++= Seq( jdbc, “mysql” % “mysql-connector-java” % “5.1.29”, “org.apache.spark” %% … Read more
customize pager in psql
From the fine psql manual: \pset option [ value ] […] pager Controls use of a pager program for query and psql help output. If the environment variable PAGER is set, the output is piped to the specified program. Otherwise a platform-dependent default (such as more) is used. When the pager option is off, the … Read more