<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: POUDEL SUNIL</title>
    <description>The latest articles on DEV Community by POUDEL SUNIL (@sunil_375).</description>
    <link>https://dev.to/sunil_375</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F375266%2F390aaafe-7cfa-4860-9e2e-2214f8ec0888.jpg</url>
      <title>DEV Community: POUDEL SUNIL</title>
      <link>https://dev.to/sunil_375</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sunil_375"/>
    <language>en</language>
    <item>
      <title>12 Most Asked PostgreSQL Questions</title>
      <dc:creator>POUDEL SUNIL</dc:creator>
      <pubDate>Fri, 03 Jul 2020 07:26:57 +0000</pubDate>
      <link>https://dev.to/truemark/12-most-asked-postgresql-questions-9gm</link>
      <guid>https://dev.to/truemark/12-most-asked-postgresql-questions-9gm</guid>
      <description>&lt;p&gt;PostgreSQL is an open-source, object-relational database management system (ORDBMS) available for all major platforms including Linux, UNIX, Windows, and OS X. it allows you to add custom functions developed using different programming languages such as C/C++, Java, etc. It is designed to handle a range of workloads, from single machines to data warehouses or Web services with many concurrent users. It is the default database for macOS Server.&lt;/p&gt;

&lt;p&gt;As developers, we have answered the most frequently asked questions about PostgreSQL and also have confirmed that the solution works perfectly. So, here is the list for PostgreSQL questions and answers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rundown For The 12 Most Asked PostgreSQL Questions
&lt;/h2&gt;

&lt;p&gt;Below are the frequently asked questions about PostgreSQL.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. How to perform “DESCRIBE TABLE” in PostgreSQL?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can try this (in the &lt;code&gt;psql&lt;/code&gt; command-line tool):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\d+ tablename
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Also,&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In addition to the PostgreSQL way (\d ‘something’ or \dt ‘table’ or \ds ‘sequence’ and so on)&lt;/p&gt;

&lt;p&gt;The SQL standard way, as shown here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select column_name, data_type, character_maximum_length
from INFORMATION_SCHEMA.COLUMNS where table_name = '&amp;lt;name of 
table&amp;gt;';
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It’s supported by many DB engines.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. How to switch databases in PostgreSQL?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In PostgreSQL, you can use the &lt;code&gt;\connect&lt;/code&gt; meta-command of the client tool psql:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\connect DBNAME
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;or in short:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\c DBNAME
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Also, you can select the database when connecting with psql. This is handy when using it from a script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo -u postgres psql -c "CREATE SCHEMA test AUTHORIZATION test;" test
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Which version of PostgreSQL are we running?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To check the current running version of PostgreSQL run this query from PostgreSQL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT version();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Also, you can use the below version to check the server version and client version of PostgreSQL&lt;/p&gt;

&lt;p&gt;Server version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pg_config --version
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Client version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;psql --version
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can also use this method&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using CLI:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Server version:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ postgres -V  # Or --version.  Use "locate bin/postgres" if not found.
postgres (PostgreSQL) 9.6.1
$ postgres -V | awk '{print $NF}'  # Last column is version.
9.6.1
$ postgres -V | egrep -o '[0-9]{1,}\.[0-9]{1,}'  # Major.Minor version
9.6
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If having more than one installation of PostgreSQL, or if getting the “&lt;code&gt;postgres: command not found&lt;/code&gt;” error:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ locate bin/postgres | xargs -i xargs -t '{}' -V  # xargs is intentionally twice.
/usr/pgsql-9.3/bin/postgres -V 
postgres (PostgreSQL) 9.3.5
/usr/pgsql-9.6/bin/postgres -V 
postgres (PostgreSQL) 9.6.1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;locate&lt;/code&gt; doesn’t help, try &lt;code&gt;find&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo find / -wholename '*/bin/postgres' 2&amp;gt;&amp;amp;- | xargs -i xargs -t '{}' -V  # xargs is intentionally twice.
/usr/pgsql-9.6/bin/postgres -V 
postgres (PostgreSQL) 9.6.1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Although &lt;code&gt;postmaster&lt;/code&gt; can also be used instead of &lt;code&gt;postgres&lt;/code&gt;, using &lt;code&gt;postgres&lt;/code&gt; is preferable because &lt;code&gt;postmaster&lt;/code&gt; is a deprecated alias of &lt;code&gt;postgres&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Client version:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As relevant, login as &lt;code&gt;postgres&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ psql -V  # Or --version
psql (PostgreSQL) 9.6.1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If having more than one installation of PostgreSQL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ locate bin/psql | xargs -i xargs -t '{}' -V  # xargs is intentionally twice.
/usr/bin/psql -V 
psql (PostgreSQL) 9.3.5
/usr/pgsql-9.2/bin/psql -V 
psql (PostgreSQL) 9.2.9
/usr/pgsql-9.3/bin/psql -V 
psql (PostgreSQL) 9.3.5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Using SQL:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Server version:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=&amp;gt; SELECT version();
                                                   version                                                    
--------------------------------------------------------------------------------------------------------------
 PostgreSQL 9.2.9 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4), 64-bit

=&amp;gt; SHOW server_version;
 server_version 
----------------
 9.2.9

=&amp;gt; SHOW server_version_num;
 server_version_num 
--------------------
 90209
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If more curious, try &lt;code&gt;=&amp;gt; SHOW all;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Client version:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For what it’s worth, a shell command can be executed within &lt;code&gt;psql&lt;/code&gt; to show the client version of the &lt;code&gt;psql&lt;/code&gt; executable in the path. Note that the running &lt;code&gt;psql&lt;/code&gt; can potentially be different from the one in the path.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=&amp;gt; \! psql -V
psql (PostgreSQL) 9.2.9
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  4. How to drop all the tables in a PostgreSQL database?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If all of your tables are in a single schema, this approach could work (below code assumes that the name of your schema is &lt;code&gt;public&lt;/code&gt;)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you are using PostgreSQL 9.3 or greater, you may also need to restore the default grants.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO public;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Another way:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can write a query to generate a SQL script like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select 'drop table "' || tablename || '" cascade;' from pg_tables;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Or:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select 'drop table if exists "' || tablename || '" cascade;' from pg_tables;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In case some tables are automatically dropped due to the cascade option in a previous sentence.&lt;/p&gt;

&lt;p&gt;Additionally, you might want to filter the tables you want to drop by schema name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select 'drop table if exists "' || tablename || '" cascade;' 
  from pg_tables
 where schemaname = 'public'; -- or any other schema
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And then run it.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. How to start the PostgreSQL server on Mac OS X?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://brew.sh/"&gt;Homebrew&lt;/a&gt; package manager includes launchctl plists to start automatically. For more information run &lt;code&gt;brew info postgres&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Start manually:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pg_ctl -D /usr/local/var/postgres start&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Stop manually:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pg_ctl -D /usr/local/var/postgres stop&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Start automatically:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;“To have launchd start PostgreSQL now and restart at login:”&lt;/p&gt;

&lt;p&gt;&lt;code&gt;brew services start postgresql&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;What is the result of &lt;code&gt;pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;What is the result of &lt;code&gt;pg_ctl -D /usr/local/var/postgres status&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;Are there any error messages in the server.log?&lt;/p&gt;

&lt;p&gt;Make sure TCP localhost connections are enabled in pg_hba.conf:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Check the listen_addresses and port in postgresql.conf:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;egrep 'listen|port' /usr/local/var/postgres/postgresql.conf&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#listen_addresses = 'localhost'     # what IP address(es) to listen on;
#port = 5432                # (change requires restart)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Cleaning up Postgres&lt;/strong&gt; was most likely installed via Homebrew, &lt;a href="http://www.finkproject.org/"&gt;Fink&lt;/a&gt;, &lt;a href="https://www.macports.org/"&gt;MacPorts&lt;/a&gt;, or the &lt;a href="https://www.enterprisedb.com/node/16#osx"&gt;EnterpriseDB&lt;/a&gt; installer.&lt;/p&gt;

&lt;p&gt;Check the output of the following commands to determine which package manager it was installed with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew &amp;amp;&amp;amp; brew list|grep postgres
fink &amp;amp;&amp;amp; fink list|grep postgres
port &amp;amp;&amp;amp; port installed|grep postgres
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Also if you want to manually start and stop PostgreSQL (installed via homebrew), the easiest way is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew services start postgresql
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew services stop postgresql
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you have a specific version, make sure to suffix the version, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew services start postgresql@10
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  6. How to change the PostgreSQL user password?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For passwordless login:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo -u user_name psql db_name
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To reset the password if you have forgotten:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER USER user_name WITH PASSWORD 'new_password';
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Option2&lt;/p&gt;

&lt;p&gt;Type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo -u postgres psql
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\password postgres
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then to quit &lt;code&gt;psql&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\q
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If that does not work, reconfigure authentication.&lt;/p&gt;

&lt;p&gt;Edit &lt;code&gt;/etc/postgresql/9.1/main/pg_hba.conf&lt;/code&gt; (the path will differ) and change:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    local   all             all                                peer
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    local   all             all                                     md5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then restart the server:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo service postgresql restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  7. How to save PL/pgSQL output from PostgreSQL to a CSV file?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Server-side&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you want something easy to re-use or automate, you can use Postgresql’s built-in &lt;a href="https://www.postgresql.org/docs/current/sql-copy.html"&gt;COPY&lt;/a&gt; command. e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Copy (Select * From foo) To '/tmp/test.csv' With CSV DELIMITER ',' HEADER;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;This approach runs entirely on the remote server&lt;/strong&gt; – it can’t write to your local PC. It also needs to be run as a Postgres “superuser” (normally called “root”) because Postgres can’t stop it doing nasty things with that machine’s local filesystem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Client-side&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The other approach is to &lt;strong&gt;do the file handling on the client-side&lt;/strong&gt;, i.e. in your application or script. The Postgres server doesn’t need to know what file you’re copying to, it just spits out the data and the client puts it somewhere.&lt;/p&gt;

&lt;p&gt;The underlying syntax for this is the &lt;code&gt;COPY TO STDOUT&lt;/code&gt; command and graphical tools like pgAdmin will wrap it for you in a nice dialog.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;psql&lt;/code&gt; command-line client has a special “meta-command” called &lt;code&gt;\copy&lt;/code&gt;, which takes all the same options as the “real” &lt;code&gt;COPY&lt;/code&gt;, but is run inside the client:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\copy (Select * From foo) To '/tmp/test.csv' With CSV
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note that there is no terminating &lt;code&gt;;&lt;/code&gt;, because meta-commands are terminated by newline, unlike SQL commands.&lt;/p&gt;

&lt;p&gt;Your application programming language may also have support for pushing or fetching the data, but you cannot generally use &lt;code&gt;COPY FROM STDIN/TO STDOUT&lt;/code&gt; within a standard SQL statement, because there is no way of connecting the input/output stream. PHP’s PostgreSQL handler (not PDO) includes very basic &lt;code&gt;pg_copy_from&lt;/code&gt; and &lt;code&gt;pg_copy_to&lt;/code&gt; functions which copy to/from a PHP array, which may not be efficient for large data sets.&lt;/p&gt;

&lt;p&gt;There are several solutions:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;a. &lt;code&gt;psql&lt;/code&gt; command&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;psql -d dbname -t -A -F"," -c "select * from users" &amp;gt; output.csv&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This has the big advantage that you can using it via &lt;code&gt;SSH, like ssh postgres@host command&lt;/code&gt; – enabling you to get&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;b. postgres &lt;code&gt;copy&lt;/code&gt; command&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;COPY (SELECT * from users) To '/tmp/output.csv' With CSV;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;c. psql interactive (or not)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;psql dbname
psql&amp;gt;\f ','
psql&amp;gt;\a
psql&amp;gt;\o '/tmp/output.csv'
psql&amp;gt;SELECT * from users;
psql&amp;gt;\q
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;All of them can be used in scripts, but you can prefer #1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;d. pgadmin but that’s not scriptable.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  8. How to create a copy of a database in PostgreSQL?
&lt;/h3&gt;

&lt;p&gt;Postgres allows the use of any existing database on the server as a template when creating a new database. You should be able to execute the following in a query window if it doesn’t:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE DATABASE newdb WITH TEMPLATE originaldb OWNER dbuser;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Still, you may get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ERROR:  source database "originaldb" is being accessed by other users
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To disconnect all other users from the database, you can use this query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity 
WHERE pg_stat_activity.datname = 'originaldb' AND pid &amp;lt;&amp;gt; pg_backend_pid();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Alternative Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To clone an existing database with Postgres you can do that&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* KILL ALL EXISTING CONNECTION FROM ORIGINAL DB (sourcedb)*/
SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity 
WHERE pg_stat_activity.datname = 'SOURCE_DB' AND pid &amp;lt;&amp;gt; pg_backend_pid();

/* CLONE DATABASE TO NEW ONE(TARGET_DB) */
CREATE DATABASE TARGET_DB WITH TEMPLATE SOURCE_DB OWNER USER_DB;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It will kill all the connection to the source DB avoiding the error&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ERROR:  source database "SOURCE_DB" is being accessed by other users
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  9. How to fix error “Fatal: role “username” does not exist”?
&lt;/h3&gt;

&lt;p&gt;Use the operating system user &lt;code&gt;postgres&lt;/code&gt; to create your database – as long as you haven’t set up a database role with the necessary privileges that corresponds to your operating system user of the same name (&lt;code&gt;h9uest&lt;/code&gt; in your case):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo -u postgres -i
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As recommended &lt;a href="https://serverfault.com/questions/359856/what-is-the-difference-between-sudo-i-and-sudo-su"&gt;here&lt;/a&gt; or &lt;a href="https://serverfault.com/questions/601140/whats-the-difference-between-sudo-su-postgres-and-sudo-u-postgres"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Then try again. Type &lt;code&gt;exit&lt;/code&gt; when done with operating as system user &lt;code&gt;postgres&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Or&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;execute the single command &lt;code&gt;createuser&lt;/code&gt; as &lt;code&gt;postgres&lt;/code&gt; with &lt;code&gt;sudo'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The point is to use the operating system user matching the database role of the same name to be granted access via &lt;code&gt;ident authentication&lt;/code&gt;. &lt;code&gt;postgres&lt;/code&gt; is the default operating system user to have initialized the database cluster. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.postgresql.org/docs/current/database-roles.html"&gt;The manual:&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In order to bootstrap the database system, a freshly initialized system always contains one predefined role. This role is always a “superuser”, and by default (unless altered when running &lt;code&gt;initdb&lt;/code&gt;) it will have the same name as the operating system user that initialized the database cluster. Customarily, this role will be named &lt;code&gt;postgres&lt;/code&gt;. In order to create more roles you first have to connect as this initial role.&lt;/p&gt;

&lt;p&gt;Read about &lt;a href="https://www.postgresql.org/docs/current/user-manag.html"&gt;database roles&lt;/a&gt; and &lt;a href="https://www.postgresql.org/docs/current/client-authentication.html"&gt; in the manual.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Also you can try this solution&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In short, running&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo -u postgres createuser owning_user
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;creates a role with name owning_user (in this case, h9uest). After that you can run &lt;code&gt;rake db:create&lt;/code&gt; from the terminal under whatever account name you set up without having to enter into the Postgres environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  10. How to fix error “Peer authentication failed for user “Postgres”?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The problem is still your &lt;code&gt;pg_hba.conf&lt;/code&gt; file (&lt;code&gt;/etc/postgresql/9.1/main/pg_hba.conf*&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;This line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;local   all             postgres                                peer
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Should be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;local   all             postgres                                md5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you can’t find this file, running &lt;code&gt;locate pg_hba.conf&lt;/code&gt; should show you where the file is.&lt;/p&gt;

&lt;p&gt;After altering this file, don’t forget to restart your PostgreSQL server. If you’re on Linux, that would be &lt;code&gt;sudo service postgresql restart&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;These are brief descriptions of both options according to the &lt;a href="https://www.postgresql.org/docs/9.3/auth-methods.html"&gt;official PostgreSQL docs on authentication methods.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Peer authentication&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The peer authentication method works by obtaining the client’s operating system user name from the kernel and using it as the allowed database user name (with optional user name mapping). This method is only supported on local connections.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Password authentication&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The password-based authentication methods are md5 and password. These methods operate similarly except for the way that the password is sent across the connection, namely MD5-hashed and clear-text respectively.&lt;/p&gt;

&lt;p&gt;If you are at all concerned about password “sniffing” attacks then md5 is preferred. A plain password should always be avoided if possible. However, md5 cannot be used with the db_user_namespace feature. If the connection is protected by SSL encryption then the password can be used safely (though SSL certificate authentication might be a better choice if one is depending on using SSL).&lt;/p&gt;

&lt;p&gt;Sample location for &lt;code&gt;pg_hba.conf&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/etc/postgresql/9.1/main/pg_hba.conf&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  11. How to drop a PostgreSQL database if there are active connections to it?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This will drop existing connections except for yours:&lt;/p&gt;

&lt;p&gt;Query&lt;code&gt;pg_stat_activity&lt;/code&gt; and get the pid values you want to kill, then issue &lt;code&gt;SELECT pg_terminate_backend(pid int)&lt;/code&gt; to them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PostgreSQL 9.2 and above:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'TARGET_DB' -- ← change this to your DB
  AND pid &amp;lt;&amp;gt; pg_backend_pid();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;PostgreSQL 9.1 and below:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT pg_terminate_backend(pg_stat_activity.procpid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'TARGET_DB' -- ← change this to your DB
  AND procpid &amp;lt;&amp;gt; pg_backend_pid();
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Once you disconnect everyone you will have to disconnect and issue the DROP DATABASE command from a connection from another database aka not the one you’re trying to drop.&lt;/p&gt;

&lt;p&gt;Note the renaming of the &lt;code&gt;procpid&lt;/code&gt; column to &lt;code&gt;pid&lt;/code&gt;. See &lt;a href="https://www.postgresql.org/message-id/E1T6wSQ-0005kN-LD@wrigleys.postgresql.org"&gt;this mailing list thread&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  12. How to Insert or perform multiple updates in PostgreSQL?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;PostgreSQL since version 9.5 has &lt;a href="https://www.postgresql.org/docs/devel/sql-insert.html"&gt;UPSERT&lt;/a&gt; syntax, with &lt;a href="https://www.postgresql.org/docs/current/sql-insert.html#SQL-ON-CONFLICT"&gt;ON CONFLICT&lt;/a&gt; clause. with the following syntax (similar to MySQL)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO the_table (id, column_1, column_2) 
VALUES (1, 'A', 'X'), (2, 'B', 'Y'), (3, 'C', 'Z')
ON CONFLICT (id) DO UPDATE 
  SET column_1 = excluded.column_1, 
      column_2 = excluded.column_2;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Searching PostgreSQL’s email group archives for “upsert” leads to finding &lt;a href="https://www.postgresql.org/docs/current/plpgsql-control-structures.html#PLPGSQL-UPSERT-EXAMPLE"&gt;an example of doing what you possibly want to do, in the manual&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example: Exceptions with UPDATE/INSERT&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This example uses exception handling to perform either UPDATE or INSERT, as appropriate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE db (a INT PRIMARY KEY, b TEXT);

CREATE FUNCTION merge_db(key INT, data TEXT) RETURNS VOID AS
$$
BEGIN
    LOOP
        -- first try to update the key
        -- note that "a" must be unique
        UPDATE db SET b = data WHERE a = key;
        IF found THEN
            RETURN;
        END IF;
        -- not there, so try to insert the key
        -- if someone else inserts the same key concurrently,
        -- we could get a unique-key failure
        BEGIN
            INSERT INTO db(a,b) VALUES (key, data);
            RETURN;
        EXCEPTION WHEN unique_violation THEN
            -- do nothing, and loop to try the UPDATE again
        END;
    END LOOP;
END;
$$
LANGUAGE plpgsql;

SELECT merge_db(1, 'david');
SELECT merge_db(1, 'dennis');
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There’s possibly an example of how to do this in bulk, using CTEs in 9.1 and above, in the &lt;a href="https://www.postgresql.org/message-id/BANLkTi=AFampn19+VrbHa7EbL45XvjUgiQ@mail.gmail.com"&gt;hackers mailing list&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;WITH foos AS (SELECT (UNNEST(%foo[])).*)
updated as (UPDATE foo SET foo.a = foos.a ... RETURNING foo.id)
INSERT INTO foo SELECT foos.* FROM foos LEFT JOIN updated USING(id)
WHERE updated.id IS NULL;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;In Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the list for most frequently asked questions with solutions about PostgreSQL. Hope this blog helped you with your problem if you haven’t found what you are looking for. Please feel free to comment if you need any help. We will get in touch with you as soon as possible.&lt;/p&gt;

</description>
      <category>postgres</category>
    </item>
  </channel>
</rss>
