Postgres ignoring search_path parameter - c#

I have a C# app connecting to a postgres database, through pg_bouncer, using Npgsql. In my connection string, I include SearchPath. Npgsql picks this up and sets the search_path parameter in the startup packet.
Pg_bouncer seems to not like that search_path parameter, which would cause the initial connection to fail (Unsupported startup parameter: search_path). To get around this, we listed it in the ignore_startup_parameters list for pg_bouncer.
The connection now gets through to the database fine, but totally ignores any SearchPath declared in the connection string. Every query now, instead of hitting the correct schema, selects out of the Public schema.
How can I get Postgres to respect the SearchPath again?

You could set a default search_path (permanently) for the role(s) you are connecting with:
ALTER ROLE foo SET search_path=blarg,public;
Or for the whole DB, depending on your exact requirements. You can even just issue a plain SET statement at the top of your session / transaction. There are multiple ways to set the search_path in Postgres. Detailed instructions:
How does the search_path influence identifier resolution and the "current schema"

Related

EF Core Seeding Data Depending on Database Name

As a part of the migration, I need to prefill an SQL Server table with the data where one of the fields should depend on the target database name or server name. At least it should not be the same for the Development and Production environments.
I wrote a code in OnModelCreating using modelBuilder.Entity<T>().HasData(...) but I still have no idea how to take the target database name here.
I think you can get database name from your connection string.
Put this code inside the Seed method:
var connection = context.Database.Connection.ConnectionString;
or something like that, which provide the connection string, then get the database name from that object.

Incorrect string value: '\xF0\x9F\x98\x80y' for column but Configured utf8mb4 already [duplicate]

In order to use 4-byte utf8mb4 in MySQL (5.6.11), I have set the following variables in the my.ini file (my.cnf is not found). This file is located in a hidden folder named Application Data (C:\Documents and Settings\All Users\Application Data\MySQL\MySQL Server 5.6) on Windows XP. It is not available under the installation directory.
[client]
port=3306
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
init-connect='SET NAMES utf8mb4'
collation_server=utf8mb4_unicode_ci
character_set_server=utf8mb4
And then issuing the following command,
SHOW VARIABLES
WHERE Variable_name
LIKE 'character\_set\_%'
OR Variable_name LIKE 'collation%';
still displays the following list.
From the picture itself, it is clear that several variables are still using 3-byte utf8.
Before doing this, the following command had already been issued to make corresponding changes to the database itself.
ALTER DATABASE database_name
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
And the following command had also been issued on each and every table in the said database.
ALTER TABLE table_name
CONVERT TO CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
Nevertheless, what is the reason why some variables have not yet been set to the said character set as well as the collation? What is missing?
The system (operating system) itself was restarted after every single task specified above had been carried out.
The client usually sets these values when connecting. The settings in my.ini are merely defaults which apply when the client does not explicitly specify a connection encoding. Since they're unreliable, every client should specify a connection encoding. Since you've got some fancy screenshot there I'll guess that you're connecting with some GUI utility which probably explicitly does set some connection encodings.
PHP example of setting a connection charset:
new PDO('mysql:host=localhost;charset=utf8mb4')
If you show your global variables, you might see that all your settings are correct actually.
SHOW GLOBAL VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';
This might be related to this bug
I also faced the same problem in the past. I changed a DB charset from utf8 to utf8mb4. Executing queries directly from mysql command line was ok but I had problem inserting emojis with Workbench. I ended up setting it manually by running
SET NAMES 'utf8mb4'
every time I open a connection to my DB with Workbench.
Using Sequel Pro as alternative was fine as well.
I think you are connecting as root, hence the init-connect='SET NAMES utf8mb4' is not executed.
It is unwise to use root (or SUPER) for any application code; just for administrative actions.
On win7
use "win + R" and type "services.msc"
find service of mysql
check the file path. It will tell you where the my.ini
open and add some properties:
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
restart the mysql service

Creating MySql Database on new install of c# application if it doesnt exist

I am about to deploy my application and have came into a bit of trouble.
I have the connection string for the database held in the application.settings and need a way to check if the database exists when the program first starts up, and if it doesn't, i need the program to create it before starting the program.
I am assuming it would be a mysql statement to check if db exists, if not create. However, I don't know where or how to do this, can I create a mysql dump of a blank database with tables etc already created and use that?
I have already stored the mysql dll files locally so there is no problem with that, its just creating the database that the string wants to connect to before the application runs so there are no connection errors straight away.
Thanks.
You can do this by running the following SQL statement:
SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = "my_db"
If it doesn't exist from the result set you get returned you can then create it.
This does pose questions regarding MySQL permissions and if your application should have user rights that enable such checking.
Edit in response of comments.
It isn’t clear if you create the connection string or not – I’ll assume the worst and that it is a part of the setup so your client can enter it (if you do know it the process below simplifies.
I would pass the connection string to the constructor of the MySqlConnectionStringBuilder class, this then makes it easy to connect to the database using the MySqlConnection class. I would use the properties from the new instance of the MySqlConnectionStringBuilder class (Server, Host, User etc) to setup the MySqlConnection class.
If the connection didn’t work I would return information to the user and they can update their connection string.
Once I’ve successfully connected to the database I would then use the database name from the Database property of my MySqlConnectionStringBuilder instance to build the query above.
If the command returns NULL the database doesn't exist and then needs creating, if the database does exist then the command will return the name of the database.
Now there are two paths:
It Doesn't exist – It needs creating, I would probably have an external SQL file with the create statements in (can be produced by MySQL dump by using the –nodata option). I would parse this file and execute the create statements
It does exist – I would now check the structure of the database to make sure it is compatible before continuing the installation.

MongoDB and C# driver socket timeouts in replica set with only one member

I have a C# web app running which connects with c# driver 1.7.0.4714 to a MongoDB. I want to put this one MongoDB into a replica set with two secondaries. When I restart my original MongoDB in replica set mode it gets the primary but then most of the connections from my web app fail with
System.Net.Sockets.SocketException
When I restart the MongoDB in non replica set mode everything works nicely again.
Here's my connection string: "mongodb://myserver:27017"
When you connect to a replica set, normally the driver can figure out that it's connecting to a replica set or a stand-alone server.
You don't say which version of the C# driver you are using, and you don't provide exact format of connection string/syntax you are using, but the tutorial covers this in detail here: http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial#CSharpDriverTutorial-MongoClientclass.
It indicates that if only one server is specified and you don't specify replica set name, then direct mode is used, which is not what you want.
You should be using either replica set mode explicitly rather than default automatic mode, or you should specify the replica set name.
I found the answer which was quite easy, sorry to ask a dump question here:
I had my replcia set set up using domain names, i.e.
rs.add("myserver01")
rs.add("myserver02")
rs.add("myserver03")
The domain names myserver01, ..., could not be resolved on my application server and I connected using IP addresses: mongodb://10.0.0.1:27017,10.0.0.2:27017,10.0.0.3:27017
So every 2nd or third request failed. Fixing the connection string to use domain names and also fixing that the domain names could be resolved fixed everything.

ADO.NET Connection String

I have a SQL Server 2008 database that uses a default schema called "Arxame". How do I specify or change the default schema from "dbo" to "Arxame" in my connection string? I'm using C# and ADO.NET.
You can't do that. You have to set the schema "Arxame" to the user you have specified on your connection string. You can do this using the SQL Server Management tool
If you need to change the default schema for an existing user you can do it like this
B. Changing the default schema of a user
The following example changes the default schema of the user Mary51 to Purchasing.
USE AdventureWorks2008R2;
ALTER USER Mary51 WITH DEFAULT_SCHEMA = Purchasing;
GO
Source: MSDN
The InitialCatalog is indeed the database name. The schema that is used would depend on the user you specify, since schemas typically map to database users. Whatever user owns the Arxame schema is the one you should specify in the connection string.
I do not believe you can do this within a connection string, nor should you be able to. You use schemas, in much the same was as a namespace in C#, to further resolve securable objects within a database when there may be name collisions.
Schemas in SQL Server 2005 and up
The initial schema must be set in your connection string:
Data Source=localhost;Initial Catalog=Arxame;Integrated Security=True
Remember to use Integrated security only if you are using a Local Sql Server.

Categories