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. 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. Rundown For The 12 Most Asked PostgreSQL Questions Below are the frequently asked questions about PostgreSQL. 1. How to perform “DESCRIBE TABLE” in PostgreSQL? Answer: You can try this (in the command-line tool): psql <code = >\d<span = >+ tablename< class " language-sql" class "token operator" </ > span /code> Also, In addition to the PostgreSQL way (\d ‘something’ or \dt ‘table’ or \ds ‘sequence’ and so on) The SQL standard way, as shown here: <code = ><span = > </span> column_name<span = >,</span> data_type<span = >,</span> character_maximum_length <span = > </span> INFORMATION_SCHEMA<span = >.</span><span = >COLUMNS</span> <span = > </span> table_name <span = >=</span> <span = > </span><span = >;</span></code> class " language-sql" class "token keyword" select class "token punctuation" class "token punctuation" class "token keyword" from class "token punctuation" class "token keyword" class "token keyword" where class "token operator" class "token string" '&lt;name of table&gt;' class "token punctuation" It’s supported by many DB engines. 2. How to switch databases in PostgreSQL? Answer: In PostgreSQL, you can use the meta-command of the client tool psql: \connect <code = >\<span = >connect< code> class " language-sql" class "token keyword" /span> DBNAME</ or in short: < = >\c DBNAME</ > code class " language-sql" code Also, you can select the database when connecting with psql. This is handy when using it from a script: <code = >sudo <span = >- u postgres psql <span = >- c <span = > < code> class " language-sql" class "token operator" </ > span class "token operator" </ > span class "token string" "CREATE SCHEMA test AUTHORIZATION test;" /span> test</ 3. Which version of PostgreSQL are we running? Answer: To check the current running version of PostgreSQL run this query from PostgreSQL: <code = > version<span = >( class " language-sql" SELECT < = > span class "token keyword" </ > span class "token punctuation" </ > span ) < = > span class "token punctuation" </ > span ; < = > span class "token punctuation" </ > span </ > code Also, you can use the below version to check the server version and client version of PostgreSQL Server version: <code =" language-sh">pg_config class --version</code> Client version: < = >psql --version</ > code class " language-sh" code You can also use this method Using CLI: Server version: <code = >$ postgres <span = >-</span>V <span = > postgres <span = >(</span>PostgreSQL<span = >)</span> <span = > </span><span = > </span> $ postgres <span = >-</span>V <span = >|</span> awk <span = > </span> <span = > <span = > </span><span = > </span> $ postgres <span = >-</span>V <span = >|</span> egrep <span = >-</span>o <span = > </span> <span = > <span = > </span></code> class " language-sql" class "token operator" class "token comment" # Or --version. Use "locate bin/postgres" not found.</span> if class "token punctuation" class "token punctuation" class "token number" 9.6 class "token number" .1 class "token operator" class "token operator" class "token string" '{print $NF}' class "token comment" # Last column is version.</span> class "token number" 9.6 class "token number" .1 class "token operator" class "token operator" class "token operator" class "token string" '[0-9]{1,}\.[0-9]{1,}' class "token comment" # Major.Minor version</span> class "token number" 9.6 If having more than one installation of PostgreSQL, or if getting the “ ” error: postgres: command not found <code = >$ locate bin<span = >/</span>postgres <span = >|</span> xargs <span = >-</span>i xargs <span = >-</span>t <span = > </span> <span = >-</span>V <span = > <span = >/</span>usr<span = >/</span>pgsql<span = >-</span><span = > </span><span = >/</span>bin<span = >/</span>postgres <span = >-</span>V postgres <span = >(</span>PostgreSQL<span = >)</span> <span = > </span><span = > </span> <span = >/</span>usr<span = >/</span>pgsql<span = >-</span><span = > </span><span = >/</span>bin<span = >/</span>postgres <span = >-</span>V postgres <span = >(</span>PostgreSQL<span = >)</span> <span = > </span><span = > </span></code> class " language-sql" class "token operator" class "token operator" class "token operator" class "token operator" class "token string" '{}' class "token operator" class "token comment" # xargs is intentionally twice.</span> class "token operator" class "token operator" class "token operator" class "token number" 9.3 class "token operator" class "token operator" class "token operator" class "token punctuation" class "token punctuation" class "token number" 9.3 class "token number" .5 class "token operator" class "token operator" class "token operator" class "token number" 9.6 class "token operator" class "token operator" class "token operator" class "token punctuation" class "token punctuation" class "token number" 9.6 class "token number" .1 If doesn’t help, try : locate find <code = >$ sudo find <span = >/</span> <span = >-</span>wholename <span = > </span> <span = > </span><span = >&gt;</span><span = >&amp;</span><span = >-</span> <span = >|</span> xargs <span = >-</span>i xargs <span = >-</span>t <span = > </span> <span = >-</span>V <span = > <span = >/</span>usr<span = >/</span>pgsql<span = >-</span><span = > </span><span = >/</span>bin<span = >/</span>postgres <span = >-</span>V postgres <span = >(</span>PostgreSQL<span = >)</span> <span = > </span><span = > </span></code> class " language-sql" class "token operator" class "token operator" class "token string" '*/bin/postgres' class "token number" 2 class "token operator" class "token operator" class "token operator" class "token operator" class "token operator" class "token operator" class "token string" '{}' class "token operator" class "token comment" # xargs is intentionally twice.</span> class "token operator" class "token operator" class "token operator" class "token number" 9.6 class "token operator" class "token operator" class "token operator" class "token punctuation" class "token punctuation" class "token number" 9.6 class "token number" .1 Although can also be used instead of , using is preferable because is a deprecated alias of . postmaster postgres postgres postmaster postgres Client version: As relevant, login as . postgres <code = >$ psql <span = >-</span>V <span = > psql <span = >(</span>PostgreSQL<span = >)</span> <span = > </span><span = > </span></code> class " language-sql" class "token operator" class "token comment" # Or --version</span> class "token punctuation" class "token punctuation" class "token number" 9.6 class "token number" .1 If having more than one installation of PostgreSQL: <code = >$ locate bin<span = >/</span>psql <span = >|</span> xargs <span = >-</span>i xargs <span = >-</span>t <span = > </span> <span = >-</span>V <span = > <span = >/</span>usr<span = >/</span>bin<span = >/</span>psql <span = >-</span>V psql <span = >(</span>PostgreSQL<span = >)</span> <span = > </span><span = > </span> <span = >/</span>usr<span = >/</span>pgsql<span = >-</span><span = > </span><span = >/</span>bin<span = >/</span>psql <span = >-</span>V psql <span = >(</span>PostgreSQL<span = >)</span> <span = > </span><span = > </span> <span = >/</span>usr<span = >/</span>pgsql<span = >-</span><span = > </span><span = >/</span>bin<span = >/</span>psql <span = >-</span>V psql <span = >(</span>PostgreSQL<span = >)</span> <span = > </span><span = > </span></code> class " language-sql" class "token operator" class "token operator" class "token operator" class "token operator" class "token string" '{}' class "token operator" class "token comment" # xargs is intentionally twice.</span> class "token operator" class "token operator" class "token operator" class "token operator" class "token punctuation" class "token punctuation" class "token number" 9.3 class "token number" .5 class "token operator" class "token operator" class "token operator" class "token number" 9.2 class "token operator" class "token operator" class "token operator" class "token punctuation" class "token punctuation" class "token number" 9.2 class "token number" .9 class "token operator" class "token operator" class "token operator" class "token number" 9.3 class "token operator" class "token operator" class "token operator" class "token punctuation" class "token punctuation" class "token number" 9.3 class "token number" .5 Using SQL: Server version: <code = > <span = >SELECT< span> version <span = >-------------------------------------------------------------------------------------------------------------- PostgreSQL <span = > < span> x86_64<span = >- unknown<span = >- linux<span = >- gnu<span = >,< span> gcc <span = >( GCC<span = >)< span> <span = > < span>Red Hat <span = > < span> <span = > < span> <span = >= <span = >SHOW< span> server_version <span = >---------------- <span = > < span> <span = >SHOW< span> server_version_num <span = >-------------------- <span = > < code> class " language-sql" = < = > span class "token operator" </ > span &gt; < = > span class "token operator" </ > span class "token keyword" /span> version<span class="token punctuation">(</ ) < = > span class "token punctuation" </ > span ; < = > span class "token punctuation" </ > span class "token comment" </ > span class "token number" 9.2 /span><span class="token number">.9</ on < = > span class "token keyword" </ > span class "token operator" </ > span class "token operator" </ > span class "token operator" </ > span class "token punctuation" /span> compiled <span class="token keyword">by</ class "token punctuation" </ > span class "token punctuation" /span> <span class="token number">4.4</ .7 < = > span class "token number" </ > span class "token number" 20120313 /span> <span class="token punctuation">(</ class "token number" 4.4 /span><span class="token number">.7</ - < = > span class "token operator" </ > span 4 < = > span class "token number" </ > span ) < = > span class "token punctuation" </ > span , < = > span class "token punctuation" </ > span class "token number" 64 /span><span class="token operator">-</ bit < = > span class "token keyword" </ > span class "token operator" </ > span &gt; < = > span class "token operator" </ > span class "token keyword" /span> server_version<span class="token punctuation">;</ class "token comment" </ > span class "token number" 9.2 /span><span class="token number">.9</ = < = > span class "token operator" </ > span &gt; < = > span class "token operator" </ > span class "token keyword" /span> server_version_num<span class="token punctuation">;</ class "token comment" </ > span class "token number" 90209 /span></ If more curious, try ;. => SHOW all Client version: For what it’s worth, a shell command can be executed within to show the client version of the executable in the path. Note that the running can potentially be different from the one in the path. psql psql psql <code = > \<span = >! psql <span = >- V psql <span = >( PostgreSQL<span = >)< span> class " language-sql" = < = > span class "token operator" </ > span &gt; < = > span class "token operator" </ > span class "token operator" </ > span class "token operator" </ > span class "token punctuation" </ > span class "token punctuation" /span> <span class="token number">9.2</ .9 < = > span class "token number" </ > span </ > code 4. How to drop all the tables in a PostgreSQL database? Answer: If all of your tables are in a single schema, this approach could work (below code assumes that the name of your schema is ). public <code = > <span = >SCHEMA< span> <span = >CREATE< span> class " language-sql" DROP < = > span class "token keyword" </ > span class "token keyword" /span> <span class="token keyword">public</ CASCADE < = > span class "token keyword" </ > span ; < = > span class "token punctuation" </ > span class "token keyword" /span> <span class="token keyword">SCHEMA</ public < = > span class "token keyword" </ > span ; < = > span class "token punctuation" </ > span </ > code If you are using PostgreSQL 9.3 or greater, you may also need to restore the default grants. <code = ><span = >GRANT</span> <span = >ALL</span> <span = >ON</span> <span = >SCHEMA</span> <span = > </span> <span = >TO</span> postgres<span = >;</span> <span = >GRANT</span> <span = >ALL</span> <span = >ON</span> <span = >SCHEMA</span> <span = > </span> <span = >TO</span> <span = > </span><span = >;</span></code> class " language-sql" class "token keyword" class "token keyword" class "token keyword" class "token keyword" class "token keyword" public class "token keyword" class "token punctuation" class "token keyword" class "token keyword" class "token keyword" class "token keyword" class "token keyword" public class "token keyword" class "token keyword" public class "token punctuation" You can write a query to generate a SQL script like this: Another way: <code =" language-sql"><span ="token keyword"> </span> <span ="token string"> </span> <span ="token operator">||</span> tablename <span ="token operator">||</span> <span ="token string"> </span> <span ="token keyword"> </span> pg_tables<span ="token punctuation">;</span></code> class class select class 'drop table "' class class class '" cascade;' class from class Or: <code =" language-sql"><span ="token keyword"> </span> <span ="token string"> </span> <span ="token operator">||</span> tablename <span ="token operator">||</span> <span ="token string"> </span> <span ="token keyword"> </span> pg_tables<span ="token punctuation">;</span></code> class class select class 'drop table if exists "' class class class '" cascade;' class from class In case some tables are automatically dropped due to the cascade option in a previous sentence. Additionally, you might want to filter the tables you want to drop by schema name: <code = ><span = > </span> <span = > </span> <span = >||</span> tablename <span = >||</span> <span = > </span> <span = > </span> pg_tables <span = > </span> schemaname <span = >=</span> <span = > </span><span = >;</span> <span = >-- or any other schema</span></code> class " language-sql" class "token keyword" select class "token string" 'drop table if exists "' class "token operator" class "token operator" class "token string" '" cascade;' class "token keyword" from class "token keyword" where class "token operator" class "token string" 'public' class "token punctuation" class "token comment" And then run it. 5. How to start the PostgreSQL server on Mac OS X? Answer: The package manager includes launchctl plists to start automatically. For more information run . Homebrew brew info postgres Start manually: pg_ctl -D /usr/local/var/postgres start Stop manually: pg_ctl -D /usr/local/var/postgres stop Start automatically: “To have launchd start PostgreSQL now and restart at login:” brew services start postgresql What is the result of ? pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start What is the result of ? pg_ctl -D /usr/local/var/postgres status Are there any error messages in the server.log? Make sure TCP localhost connections are enabled in pg_hba.conf: <code = > host <span = >all< span> trust< class " language-sql" # IPv4 local connections: < = > span class "token comment" </ > span class "token keyword" /span> <span class="token keyword">all</ 127.0 < = > span class "token number" </ > span .0 < = > span class "token number" </ > span .1 < = > span class "token number" </ > span / < = > span class "token operator" </ > span 32 < = > span class "token number" </ > span /code> Check the listen_addresses and port in postgresql.conf: egrep 'listen|port' /usr/local/var/postgres/postgresql.conf < = ><span = >#listen_addresses = # what IP address(es) to listen on;</span> <span = >#port = # (change requires restart)</span></ > code class " language-sql" class "token comment" 'localhost' class "token comment" 5432 code was most likely installed via , , , or the installer. Cleaning up Postgres Homebrew Fink MacPorts EnterpriseDB Check the output of the following commands to determine which package manager it was installed with: <code = >brew <span = >&amp;&amp; brew list<span = >| grep postgres fink <span = >&amp;&amp; fink list<span = >| grep postgres port <span = >&amp;&amp; port installed<span = >| grep postgres< class " language-sql" class "token operator" </ > span class "token operator" </ > span class "token operator" </ > span class "token operator" </ > span class "token operator" </ > span class "token operator" </ > span /code> Also if you want to manually start and stop PostgreSQL (installed via homebrew), the easiest way is: <code = >brew services <span = >start< code> class " language-sql" class "token keyword" /span> postgresql</ and < class= > services stop postgresql</ > code " language-sql" brew code If you have a specific version, make sure to suffix the version, for example: brew services start postgresql @10 < = > code class " language-sql" < = > span class "token keyword" </ > span < = > span class "token variable" </ > span </ > code 6. How to change the PostgreSQL user password? Answer: For passwordless login: < = >sudo -u user_name psql db_name</ > code class "" code To reset the password if you have forgotten: <code =""> user_name ;</code> class ALTER USER WITH PASSWORD 'new_password' Option2 Type: < = >$ sudo -u postgres psql</ > code class "" code Then: <code ="">\ </ > class password postgres code Then to quit : psql <code class= >\ "" q</code> If that does not work, reconfigure authentication. Edit (the path will differ) and change: /etc/postgresql/9.1/main/pg_hba.conf <code =""> </ > class local all all peer code to: <code =""> </ > class local all all md5 code Then restart the server: <code ="">$ </ > class sudo service postgresql restart code 7. How to save PL/pgSQL output from PostgreSQL to a CSV file? Answer: Server-side If you want something easy to re-use or automate, you can use Postgresql’s built-in command. e.g. COPY <code =""> ( * foo) CSV ;</code> class Copy Select From To '/tmp/test.csv' With DELIMITER ',' HEADER – 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. This approach runs entirely on the remote server Client-side The other approach is to , 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. do the file handling on the client-side The underlying syntax for this is the command and graphical tools like pgAdmin will wrap it for you in a nice dialog. COPY TO STDOUT The has a special “meta-command” called , which takes all the same options as the “real” , but is run inside the client: psql command-line client \copy COPY <code ="">\ ( * foo) CSV</code> class copy Select From To '/tmp/test.csv' With Note that there is no terminating , because meta-commands are terminated by newline, unlike SQL commands. ; Your application programming language also have support for pushing or fetching the data, but you cannot generally use within a standard SQL statement, because there is no way of connecting the input/output stream. PHP’s PostgreSQL handler ( PDO) includes very basic and functions which copy to/from a PHP array, which may not be efficient for large data sets. may COPY FROM STDIN/TO STDOUT not pg_copy_from pg_copy_to There are several solutions: a. psql command psql -d dbname -t -A -F"," -c "select * from users" > output.csv This has the big advantage that you can using it via SSH, like – enabling you to get. ssh postgres@host command b. postgres copy command COPY (SELECT * from users) To '/tmp/output.csv' With CSV; c. psql interactive (or not) <code class= >& ;psql dbname psql& ;\f psql& ;\a psql& ;\o psql& ;SELECT * from users; psql& ;\ "" gt gt ',' gt gt '/tmp/output.csv' gt gt q</code> All of them can be used in scripts, but you can prefer #1. d. pgadmin but that’s not scriptable. 8. How to create a copy of a database in PostgreSQL? Answer: 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: <code =""> newdb originaldb dbuser;</code> class CREATE DATABASE WITH TEMPLATE OWNER Still, you may get: <code =""> " " </ > class ERROR: source database originaldb is being accessed by other users code To disconnect all other users from the database, you can use this query: <code =""> pg_terminate_backend(pg_stat_activity.pid) pg_stat_activity pg_stat_activity.datname = pid &lt;&gt; pg_backend_pid();</code> class SELECT FROM WHERE 'originaldb' AND Alternative Solution: To clone an existing database with Postgres you can do that <code =""> pg_terminate_backend(pg_stat_activity.pid) pg_stat_activity pg_stat_activity.datname = pid &lt;&gt; pg_backend_pid(); TARGET_DB SOURCE_DB USER_DB;</code> class /* KILL ALL EXISTING CONNECTION FROM ORIGINAL DB (sourcedb)*/ SELECT FROM WHERE 'SOURCE_DB' AND /* CLONE DATABASE TO NEW ONE(TARGET_DB) */ CREATE DATABASE WITH TEMPLATE OWNER It will kill all the connection to the source DB avoiding the error <code =""> " " </ > class ERROR: source database SOURCE_DB is being accessed by other users code 9. How to fix error “Fatal: role “username” does not exist”? Answer: Use the to create your database – as long as you haven’t set up a role with the necessary privileges that corresponds to your operating system user of the same name ( in your case): operating system user postgres database h9uest < = >sudo -u postgres -i</ > code class "" code As recommended or . here here Then try again. Type when done with operating as system user . exit postgres Or execute the single command as with createuser postgres sudo' The point is to use the operating system user matching the database role of the same name to be granted access via . is the default operating system user to have initialized the database cluster. ident authentication postgres The manual: 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 ) it will have the same name as the operating system user that initialized the database cluster. Customarily, this role will be named . In order to create more roles you first have to connect as this initial role. initdb postgres Read about and in the manual. database roles client authentication Also you can try this solution In short, running < = >sudo -u postgres createuser owning_user</ > code class "" code creates a role with name owning_user (in this case, h9uest). After that you can run from the terminal under whatever account name you set up without having to enter into the Postgres environment. rake db:create 10. How to fix error “Peer authentication failed for user “Postgres”? Answer: The problem is still your file ( ). pg_hba.conf /etc/postgresql/9.1/main/pg_hba.conf* This line: <code =""> </ > class local all postgres peer code Should be: <code =""> </ > class local all postgres md5 code If you can’t find this file, running locate should show you where the file is. pg_hba.conf After altering this file, don’t forget to restart your PostgreSQL server. If you’re on Linux, that would be . sudo service postgresql restart These are brief descriptions of both options according to the . official PostgreSQL docs on authentication methods Peer authentication 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. Password authentication 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. 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). Sample location for : pg_hba.conf /etc/postgresql/9.1/main/pg_hba.conf 11. How to drop a PostgreSQL database if there are active connections to it? Answer: This will drop existing connections except for yours: Query and get the pid values you want to kill, then issue to them. pg_stat_activity SELECT pg_terminate_backend(pid int) PostgreSQL 9.2 and above: <code =""> pg_terminate_backend(pg_stat_activity.pid) pg_stat_activity pg_stat_activity.datname = pid &lt;&gt; pg_backend_pid();</code> class SELECT FROM WHERE 'TARGET_DB' -- ← change this to your DB AND PostgreSQL 9.1 and below: <code =""> pg_terminate_backend(pg_stat_activity.procpid) pg_stat_activity pg_stat_activity.datname = procpid &lt;&gt; pg_backend_pid();</code> class SELECT FROM WHERE 'TARGET_DB' -- ← change this to your DB AND 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. Note the renaming of the column to . See . procpid pid this mailing list thread 12. How to Insert or perform multiple updates in PostgreSQL? Answer: PostgreSQL since version 9.5 has syntax, with with the following syntax (similar to MySQL) UPSERT ON CONFLICT clause. <code =""> the_table (id, column_1, column_2) ( , , ), ( , , ), ( , , ) (id) column_1 = excluded.column_1, column_2 = excluded.column_2;</code> class INSERT INTO VALUES 1 'A' 'X' 2 'B' 'Y' 3 'C' 'Z' ON CONFLICT DO UPDATE SET Searching PostgreSQL’s email group archives for “upsert” leads to finding : an example of doing what you possibly want to do, in the manual Example: Exceptions with UPDATE/INSERT This example uses exception handling to perform either UPDATE or INSERT, as appropriate: <code =""> db (a , b ); merge_db(key , data ) $$ db b = data a = key; ; ; db(a,b) (key, data); ; ; ; ; $$ plpgsql; merge_db( , ); merge_db( , );</code> class CREATE TABLE INT PRIMARY KEY TEXT CREATE FUNCTION INT TEXT RETURNS VOID AS BEGIN LOOP -- first try to update the key -- note that "a" must be unique UPDATE SET WHERE 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 VALUES RETURN EXCEPTION WHEN unique_violation THEN -- do nothing, and loop to try the UPDATE again END END LOOP END LANGUAGE SELECT 1 'david' SELECT 1 'dennis' There’s possibly an example of how to do this in bulk, using CTEs in 9.1 and above, in the : hackers mailing list <code class="">WITH foos AS ( ( (%foo[])).*) updated ( foo foo.a = foos.a ... foo.id) foo foos.* foos updated (id) updated.id ;</code> SELECT UNNEST as UPDATE SET RETURNING INSERT INTO SELECT FROM LEFT JOIN USING WHERE IS NULL In Conclusion This is the list for most frequently asked questions with solutions about PostgreSQL. I 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. This article was also posted on DevPostbyTruemark .