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

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

tech