title: How to select a database in Postgres with PHP?
body: <p>I have the following string connection:</p>
  
  <pre><code>$string_connection = "host=localhost port=5432 user=postgres password=postgres";
  $conn = pg_connect($string_connection );
  </code></pre>
  
  <p>Later in my code, I need to select a database:</p>
  
  <pre><code>pg_connect("dbname=my_database");
  </code></pre>
  
  <p>But, always I have the following error:</p>
  
  <pre><code>Warning: pg_connect(): Unable to connect to PostgreSQL server: fe_sendauth: no password supplied in ..\class.databases.php on line 142
  </code></pre>
  
  <p>But, in the example of PHP.net, they do this.
  Another example:</p>
  
  <pre><code>pg_query($conn,'\connect my_database');
  </code></pre>
  
  <p>Why this dont work?</p>
  
  <p>When I do a backup with PgAdmin, I got the following strings:</p>
  
  <pre><code>SET statement_timeout = 0;
  SET client_encoding = 'UTF8';
  SET standard_conforming_strings = off;
  SET check_function_bodies = false;
  SET client_min_messages = warning;
  SET escape_string_warning = off;
  
  DROP DATABASE my_database;
  
  CREATE DATABASE my_database WITH ENCODING = 'UTF8' LC_COLLATE = 'Portuguese_Brazil.1252' LC_CTYPE = 'Portuguese_Brazil.1252';
  
  ALTER DATABASE my_database OWNER TO postgres;
  
  \connect my_database
  </code></pre>
  
  <p>Whe I try to run this script with pg_query, I have erros:</p>
  
  <pre><code>Warning: pg_query(): Query failed: ERRO: syntax error in "\" LINE 1: \connect my_database ^
  </code></pre>
  
  <p>I need to select a dabatase to create a schema then create my tables... I dont know how to do this with Postgres.</p>